코딩,문제풀이/SWExpertAcademy
1868. 파핑파핑 지뢰찾기(D4)
DdoubleJ
2018. 8. 7. 23:41
8방향을 탐색하며 지뢰찾기를 구현하면 된다.
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 | #include <stdio.h> #include <memory.h> int n; char map[301][301]; const int dx[8] = { -1, -1, -1, 0, 1, 1, 1, 0}; const int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1}; int* check(int x, int y, int arr[]) { int result = 1; for (int i = 0; i < 8; i++) { if ((x + dx[i] < 0 || y + dy[i] < 0 || x + dx[i] >= n || y + dy[i] >= n) || map[x + dx[i]][y + dy[i]] == '.' || (map[x + dx[i]][y + dy[i]] >= '0' && map[x + dx[i]][y + dy[i]] <= '9')) { } else { arr[i] = 1; arr[8]++; } } return arr; } void change(int x, int y)//8방향이 다 없으면 바꾸고 들어감 { if (map[x][y] == '0') return; int result[9] = { 0, }; check(x, y, result); if (result[8] == 0) { map[x][y] = '0'; for (int i = 0; i < 8; i++) { if (x + dx[i] < 0 || y + dy[i] < 0 || x + dx[i] >= n || y + dy[i] >= n) continue; change(x + dx[i], y + dy[i]); } } else {//어느 뱡향엔가 지뢰가 있음 map[x][y] = result[8]+'0'; } } int main(void) { int T; setbuf(stdout, NULL); scanf("%d\n", &T); for (int test_case = 1; test_case <= T; test_case++) { memset(map, 0, sizeof(map)); scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%s", map[i]); } int cnt = 0; int x = 0, y = 0; int result[9] = { 0, }; while (true) { int cc = 1; for (x = 0; x < n; x++) { for (y = 0; y < n; y++) { if (map[x][y] == '0') continue; if (map[x][y] == '.') { check(x, y, result); if (result[8] == 0) { change(x, y); cnt++; cc = 0; } memset(result, 0, sizeof(result)); } } } if (!cc) { for (x = 0; x < n; x++) { for (y = 0; y < n; y++) { if (map[x][y] == '.') cnt++; } } break; } } printf("#%d %d\n", test_case, cnt); } return 0; } | cs |