https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5-BEE6AK0DFAVl
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int t1=0, t2=0;
vector<pair<int, int>> wall;
struct INFO { int x, y, w1, w2; };
vector<INFO> p;
vector<int> stair[2];
int answer = 2e9;
void CalAns() {
int ans1 = 0;
int ans2 = 0;
vector<int> s[2];
s[0] = stair[0];
s[1] = stair[1];
sort(s[0].begin(), s[0].end());
sort(s[1].begin(), s[1].end());
if (s[0].size() > 0) {
int id;
if (s[0].size() % 3 == 0) id = 2;
else id = s[0].size() % 3 - 1;
for (; id < s[0].size(); id += 3) {
if (ans1 > s[0][id]) {
ans1 += t1;
}
else {
ans1 = s[0][id];
ans1 += t1 + 1;
}
}
}
if (s[1].size() > 0) {
int id;
if (s[1].size() % 3 == 0) id = 2;
else id = s[1].size() % 3 - 1;
for (; id < s[1].size(); id += 3) {
if (ans2 > s[1][id]) {
ans2 += t2;
}
else {
ans2 = s[1][id];
ans2 += t2 + 1;
}
}
}
if (s[0].size() == 0) ans1 = ans2;
else if (s[1].size() == 0) ans1 = ans1;
else ans1 = max(ans1, ans2);
answer = min(answer, ans1);
}
void SelectStair(int index) {
if (index == p.size()) {
CalAns();
return;
}
stair[0].push_back(p[index].w1);
SelectStair(index + 1);
stair[0].pop_back();
stair[1].push_back(p[index].w2);
SelectStair(index + 1);
stair[1].pop_back();
}
int main(void) {
int testcase;
int index = 1;
cin >> testcase;
for (; testcase > 0; testcase--) {
answer = 2e9;
cin >> n;
wall.clear();
p.clear();
stair[0].clear();
stair[1].clear();
t1 = 0;
t2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int n;
cin >> n;
if (n > 1) {
wall.push_back({ i,j });
if (t1 == 0) t1 = n;
else t2 = n;
}
else if (n == 1) p.push_back({ i,j,0,0 });
}
}
for (int i = 0; i < p.size(); i++) {
int ii = p[i].x;
int jj = p[i].y;
int w1 = abs(ii - wall[0].first) + abs(jj - wall[0].second);
int w2 = abs(ii - wall[1].first) + abs(jj - wall[1].second);
p[i] = { ii,jj,w1,w2 };
}
SelectStair(0);
cout << "#" << index << " " << answer << "\n";
index++;
}
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
등산로 조성 C++ (0) | 2022.04.29 |
---|---|
탈주범 검거 C++ (0) | 2022.04.28 |
미생물 격리 C++ (0) | 2022.04.25 |
무선 충전 C++ (0) | 2022.04.25 |
보물상자 비밀번호 (0) | 2022.04.24 |