주어진 문자의 역할대로 코드를 구현하면 된다.
여기서 주의할 점은, '?'는 무작위 방향이 되는데, 이것 때문에 문제가 발생한다.
나는 DFS를 통해 해당좌표가 일정 횟수 이상 들어가거나 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include <stdio.h> char map[21][21]; int visit[21][21]; int r, c; int dfs(int x, int y,int direction, int memory, int count) {//0 오른 1 아래 2 왼 3위 count->dfs호출횟수 // printf("%d %d %d\n", x,y, memory); visit[x][y]++; if (map[x][y] == '@') { return 1; } else if (visit[x][y] > 500) { return 0; } else if (count > 3000) {//일정 수 이상 dfs 호출되면 탈출 불가능 return 0; } else if (map[x][y] == '<') { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } else if (map[x][y] == '>') { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else if (map[x][y] == '^') { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } else if (map[x][y] == 'v') { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else if (map[x][y] == '_') { if (memory == 0) { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } } else if (map[x][y] == '|') { if (memory == 0) { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } } else if (map[x][y] == '?') {//4방향 다 해봐야함 int check = 0; int result = 0; if (y + 1 >= c) { y = -1; check = 1; } result = dfs(x, y + 1, 0, memory, count+1); if (check == 1) { y = c - 1; } if (result == 1) { return result; } check = 0; if (x + 1 >= r) { x = -1; check = 1; } result = dfs(x + 1, y, 1, memory, count+1); if (check == 1) { x = r - 1; } if (result == 1) { return result; } check = 0; if (y - 1 < 0) { y = c; check = 1; } result = dfs(x, y - 1, 2, memory, count+1); if (check == 1) { y = 0; } if (result == 1) { return result; } check = 0; if (x - 1 < 0) { x = r; check = 1; } result = dfs(x - 1, y, 3, memory, count+1); if (result == 1) { return result; } } else if (map[x][y] == '.') { if (direction == 0) { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else if (direction == 1) { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else if (direction == 2) { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } else if (direction == 3) { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } } else if (map[x][y] >= '0' && map[x][y] <= '9') { memory = map[x][y] - '0'; if (direction == 0) { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else if (direction == 1) { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else if (direction == 2) { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } else if (direction == 3) { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } } else if (map[x][y] == '+') { if (memory == 15) memory = 0; else memory++; if (direction == 0) { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else if (direction == 1) { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else if (direction == 2) { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } else if (direction == 3) { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } } else if (map[x][y] == '-') { if (memory == 0) memory = 15; else memory--; if (direction == 0) { if (y + 1 >= c) y = -1; return dfs(x, y + 1, 0, memory, count+1); } else if (direction == 1) { if (x + 1 >= r) x = -1; return dfs(x + 1, y, 1, memory, count+1); } else if (direction == 2) { if (y - 1 < 0) y = c; return dfs(x, y - 1, 2, memory, count+1); } else if (direction == 3) { if (x - 1 < 0) x = r; return dfs(x - 1, y, 3, memory, count+1); } } return 0; } int main(void) { int T; scanf("%d\n", &T); for (int testCase = 1; testCase <= T; testCase++) { scanf("%d %d\n", &r,&c); int check = 0; for (int i = 0; i < 21; i++) { for (int j = 0; j < 21; j++) { map[i][j] = 0; visit[i][j] = 0; } } for (int i = 0; i < r; i++) { scanf("%s", map[i]); for (int j = 0; check==0 && j < c; j++) { if (map[i][j] == '@') { check = 1; break; } } } printf("#%d", testCase); if (!check) { printf(" NO\n"); continue; } int result = dfs(0, 0, 0, 0,0); if (result == 1) { printf(" YES\n"); } else { printf(" NO\n"); } } return 0; } | cs |
'코딩,문제풀이 > SWExpertAcademy' 카테고리의 다른 글
1865. 동철이의 일 분배(D4) (0) | 2018.08.07 |
---|---|
1861. 정사각형 방(D4) (0) | 2018.08.07 |
1808. 지희의 고장난 계산기(D4) (0) | 2018.08.07 |
1803. Shortest Path Faster(D4) (0) | 2018.08.07 |
1486. 장훈이의 높은 선반(D4) (0) | 2018.08.07 |