알고리즘 문제/BOJ
9207번 페그 솔리테어
parkit
2019. 4. 30. 11:41
728x90
반응형
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <iostream> #include <deque> #include <list> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #include <functional> #include <unordered_map> #include <unordered_set> #include <tuple> #include <sstream> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; char Map[6][10]; bool use[6][10] = { false, }; vector<pair<int, int> > v; int dy[4] = { 0, 1, 0, -1 }; int dx[4] = { 1, 0, -1, 0 }; int MIN = 0, moving = 0; int chk() { int ret = 0; for (int i = 0; i < 5; i++) for (int j = 0; j < 9; j++) if (Map[i][j] == 'o') ++ret; return ret; } void simulation(int cnt) { int oc = chk(); if (MIN > oc) { MIN = oc; moving = cnt; } for (int i = 0; i < 5; i++) for (int j = 0; j < 9; j++) if (Map[i][j] == 'o') for (int k = 0; k < 4; k++) { int y = i + dy[k]; int x = j + dx[k]; int ny = i + 2 * dy[k]; int nx = j + 2 * dx[k]; if (ny < 0 || ny >= 5 || nx < 0 || nx >= 9) continue; if (Map[y][x] == 'o' && Map[ny][nx] == '.') { Map[i][j] = '.'; Map[y][x] = '.'; Map[ny][nx] = 'o'; simulation(cnt + 1); Map[i][j] = 'o'; Map[y][x] = 'o'; Map[ny][nx] = '.'; } } } int main(void) { int T = 0; scanf("%d", &T); while (T--) { MIN = 2e9; moving = 0; v.clear(); for (int i = 0; i < 5; i++) for (int j = 0; j < 9; j++) cin >> Map[i][j]; simulation(0); printf("%d %d\n", MIN, moving); } return 0; } | cs |
728x90
반응형