ALGORITHM
백준 / 9207번 / 페그 솔리테어 / C++
chrisysl
2019. 2. 20. 15:10
# 출처 : https://www.acmicpc.net/problem/9207
백트래킹을 이용하는 문제인데.. 나중에 다시풀어봐야겠다.
# 풀이
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <iostream> #include <algorithm> #define endl '\n' using namespace std; int N, pin_cnt, move_cnt; char map[6][10]; const int dR[] = { 1, 0, -1, 0 }; const int dC[] = { 0, 1, 0, -1 }; bool inRange(int row, int col) { return (row >= 0 && row < 5) && (col >= 0 && col < 9); } void DFS(int depth, int cnt) { for (int r = 0; r < 5; ++r) { for (int c = 0; c < 9; ++c) { int cR = r, cC = c; if (map[cR][cC] == 'o') { for (int i = 0; i < 4; ++i) { int nR = cR + dR[i]; int nC = cC + dC[i]; if (inRange(nR, nC) && map[nR][nC] == 'o') { int nnR = nR + dR[i]; int nnC = nC + dC[i]; if (inRange(nnR, nnC) && map[nnR][nnC] == '.') { map[cR][cC] = '.'; map[nR][nC] = '.'; map[nnR][nnC] = 'o'; DFS(depth + 1, cnt - 1); map[cR][cC] = 'o'; map[nR][nC] = 'o'; map[nnR][nnC] = '.'; } } } } } } if (cnt <= pin_cnt) { if (cnt == pin_cnt) move_cnt = min(move_cnt, depth); else move_cnt = depth; pin_cnt = cnt; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N; while (N--) { int cnt = 0; for (int i = 0; i < 5; i++) { cin >> map[i]; for (int j = 0; j < 9; j++) if (map[i][j] == 'o') cnt++; } pin_cnt = cnt, move_cnt = 0; DFS(0, cnt); cout << pin_cnt << " " << move_cnt << endl; } return 0; } | cs |