6중 For문 대신 DFS로~
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include <stdio.h> #include <string.h> typedef struct { int y; int x; }point; point Virus[12]; int map[10][10]; int V[10][10]; int C, R, Virus_num, MAX; int dx[] = { 0,0,-1,1 }; int dy[] = { -1,1,0,0 }; void Build_Wall(int y, int x, int num) { V[y][x] = 1; if (num == 0) { int visit[10][10] = { 0, }; int m[10][10]; for (int a = 0; a < 10; a++) for (int b = 0; b < 10; b++) m[a][b] = map[a][b]; for (int i = 0; i < Virus_num; i++) { int Q_Front = -1, Q_Back = -1; point Q[8 * 8] = { 0, }; Q[++Q_Back].y = Virus[i].y; Q[Q_Back].x = Virus[i].x; while (Q_Back != Q_Front) { int y = Q[++Q_Front].y; int x = Q[Q_Front].x; for (int a = 0; a < 4; a++) { int yy = y + dy[a]; int xx = x + dx[a]; if (m[yy][xx] == 0 && visit[yy][xx] == 0) { m[yy][xx] = 2; visit[yy][xx] = 1; Q[++Q_Back].y = yy; Q[Q_Back].x = xx; } } } } int zero_count = 0; for (int a = 1; a <= C; a++) { for (int b = 1; b <= R; b++) { if (m[a][b] == 0) zero_count++; } } if (zero_count > MAX) MAX = zero_count; return; } for (int i = 1; i <= C; i++) { for (int j = 1; j <= R; j++) { if (map[i][j] == 0 && V[i][j]==0) { map[i][j] = 1; Build_Wall(i, j, num - 1); map[i][j] = 0; } } } V[y][x] = 0; return; } int main() { memset(map, -1, sizeof(map)); Virus_num = 0; MAX = -1; scanf("%d %d", &C, &R); for (int i = 1; i <= C; i++) { for (int j = 1; j <= R; j++) { scanf("%d",&map[i][j]); if (map[i][j] == 2) { Virus[Virus_num].y = i; Virus[Virus_num++].x = j; } } } for (int i = 1; i <= C; i++) { for (int j = 1; j <= R; j++) { if (map[i][j] == 0) { map[i][j] = 1; Build_Wall(i, j, 2); map[i][j] = 0; } } } printf("%d\n",MAX); return 0; } | cs |
'SW 업무 관련 > 백준' 카테고리의 다른 글
[C언어] 25686 치킨 배달. (0) | 2018.09.24 |
---|---|
[C언어] 6593 상범 빌딩 (0) | 2018.09.24 |
[C언어]14502. 연구소 (0) | 2018.09.17 |
[C언어]14503. 로봇 청소기 (0) | 2018.09.16 |
[C언어]2573. 빙산 (0) | 2018.09.16 |