알고리즘 문제/AlgoSpot
게임판 덮기(BOARDCOVER)
parkit
2019. 1. 6. 20:10
728x90
반응형
https://algospot.com/judge/problem/read/BOARDCOVER
구현을 하고, 계속 어디서 틀렸나 봤더니
백트래킹 함수 안에 이중 for문으로 계속 돌고 있었다.
행과 열에 관한 이중 for문이 안에 있으면 안 된다.
다 채워지는 그 판에 대한 경우의 수가 '중복'되어 더해지기 때문이다.
참고로, coverType 배열은 아래 그림과 같다.
노란색 별 ★ = (0, 0)이고, (행, 열) = (y, x)이다.
<틀린 코드, 백트래킹 함수 안에 이중 for문>
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 107 108 109 110 111 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; // 책 162p const int coverType[4][3][2] = { { { 0, 0 },{ 1, 0 },{ 0, 1 } }, { { 0, 0 },{ 0, 1 },{ 1, 1 } }, { { 0, 0 },{ 1, 0 },{ 1, 1 } }, { { 0, 0 },{ 1, 0 },{ 1,-1 } } }; char Map[21][21]; int H = 0, W = 0; bool visit[21][21] = { false, }; int backtracking(int y, int x) { int h = -1, w = -1; bool stop = false; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (Map[i][j] == '#') continue; if (!visit[i][j]) { h = i; w = j; stop = true; break; } } if (stop) break; } if (h == -1 && w == -1) return 1; int ret = 0; for (int i = h; i < H; i++) { for (int j = w; j < W; j++) { if (visit[i][j] || Map[i][j] == '#') continue; for (int a = 0; a < 4; a++) { int fy = i + coverType[a][0][0]; int fx = j + coverType[a][0][1]; int sy = i + coverType[a][1][0]; int sx = j + coverType[a][1][1]; int ty = i + coverType[a][2][0]; int tx = j + coverType[a][2][1]; if (sy < 0 || sy >= H || sx < 0 || sx >= W || visit[sy][sx] || Map[sy][sx] == '#') continue; if (ty < 0 || ty >= H || tx < 0 || tx >= W || visit[ty][tx] || Map[ty][tx] == '#') continue; visit[fy][fx] = visit[sy][sx] = visit[ty][tx] = true; ret += backtracking(i, j); visit[fy][fx] = visit[sy][sx] = visit[ty][tx] = false; } } } return ret; } int main(void) { int emptyCount = 0; scanf("%d %d", &H, &W); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> Map[i][j]; if (Map[i][j] == '.') ++emptyCount; } } if (emptyCount % 3 != 0) printf("0\n"); else printf("%d\n", backtracking(0, 0)); return 0; } | cs |
<정답 코드>
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 107 108 109 110 111 112 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; // 책 162p const int coverType[4][3][2] = { {{0, 0}, {1, 0}, {0, 1}}, {{0, 0}, {0, 1}, {1, 1}}, {{0, 0}, {1, 0}, {1, 1}}, {{0, 0}, {1, 0}, {1,-1}} }; char Map[21][21]; int H = 0, W = 0; bool visit[21][21] = { false, }; int backtracking() { int h = -1, w = -1; bool stop = false; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (Map[i][j] == '#') continue; if (!visit[i][j]) { h = i; w = j; stop = true; break; } } if (stop) break; } if (h == -1 && w == -1) return 1; int ret = 0; for (int a = 0; a < 4; a++) { int fy = h + coverType[a][0][0]; int fx = w + coverType[a][0][1]; int sy = h + coverType[a][1][0]; int sx = w + coverType[a][1][1]; int ty = h + coverType[a][2][0]; int tx = w + coverType[a][2][1]; if (sy < 0 || sy >= H || sx < 0 || sx >= W || visit[sy][sx] || Map[sy][sx] == '#') continue; if (ty < 0 || ty >= H || tx < 0 || tx >= W || visit[ty][tx] || Map[ty][tx] == '#') continue; visit[fy][fx] = visit[sy][sx] = visit[ty][tx] = true; ret += backtracking(); visit[fy][fx] = visit[sy][sx] = visit[ty][tx] = false; } return ret; } int main(void) { int T = 0, emptyCount = 0; scanf("%d", &T); while (T--) { memset(visit, false, sizeof(visit)); emptyCount = 0; scanf("%d %d", &H, &W); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> Map[i][j]; if (Map[i][j] == '.') ++emptyCount; } } if (emptyCount % 3 != 0) printf("0\n"); else printf("%d\n", backtracking()); } return 0; } | cs |
728x90
반응형