알고리즘 문제/BOJ
6593번 상범 빌딩
parkit
2018. 8. 24. 00:00
728x90
반응형
기본적인 BFS 문제이다.
다만, 3차원 유형의 BFS이다.
동,서,남,북,상,하이므로, 총 6개를 선언해야함을 알 수 있다.(코드 21 ~ 23번 째 줄)
그리고, 102번 째 줄에 getchar();는 Enter를 map[i][j][h]가 인식할까봐 넣었다.(지우고 채점을 안 했음.)
queue와 visit는 굳이 전역으로 선언할 필요가 없다.
map[행][열][높이]
visit[행][열][높이]
https://www.acmicpc.net/problem/6593
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 <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; typedef struct Building { int y = 0; int x = 0; int z = 0; }Building; int L = 0, R = 0, C = 0; int dy[6] = { -1, 0, 1, 0, 0, 0 }; int dx[6] = { 0, -1, 0, 1, 0, 0 }; int dz[6] = { 0, 0, 0, 0, 1, -1 }; char map[31][31][31]; int BFS(int r, int c, int z) { queue<Building> q; bool visit[31][31][31] = { false, }; memset(visit, false, sizeof(visit)); q.push({ r, c, z }); visit[r][c][z] = true; int ret = 0; while (!q.empty()) { int qSize = q.size(); while (qSize--) { int y = q.front().y; int x = q.front().x; int z = q.front().z; q.pop(); if (map[y][x][z] == 'E') { return ret; } for (int i = 0; i < 6; i++) { int ny = y + dy[i]; int nx = x + dx[i]; int nz = z + dz[i]; if (0 <= ny && ny < R && 0 <= nx && nx < C && 0 <= nz && nz < L && !visit[ny][nx][nz] && map[ny][nx][nz] != '#') { visit[ny][nx][nz] = true; q.push({ ny, nx, nz }); } } } ++ret; } return -1; } int main(void) { while (scanf("%d %d %d", &L, &R, &C) && L != 0 && R != 0 && C != 0) { int sy = 0, sx = 0, sz = 0; for(int h = 0; h < L; h++) { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { cin >> map[i][j][h]; if (map[i][j][h] == 'S') { sx = j; sy = i; sz = h; } } } getchar(); } int ans = BFS(sy, sx, sz); if (ans != -1) printf("Escaped in %d minute(s).\n", ans); else printf("Trapped!\n"); } return 0; } | cs |
728x90
반응형