반응형

https://www.acmicpc.net/problem/6593

 

6593번: 상범 빌딩

당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 빌딩의 상태를 입력받고, 시작점의 좌표를 저장합니다.

 

2. bfs를 이용하여 S에서 시작해서 E에 도착할 때까지의 시간을 출력합니다.

 

3. 만약 도착할 수 없다면 Trapped!를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<queue>
 
using namespace std;
 
int L, R, C;
char arr[31][31][31];
int dl[6= { 0,0,0,0,-1,1 };
int dr[6= { -1,1,0,0,0,0 };
int dc[6= { 0,0,-1,1,0,0 };
int Sl, Sr, Sc, El, Er, Ec;
 
struct node {
    int l;
    int r;
    int c;
    int cnt;
};
 
int main()
{
    while (1) {
        scanf("%d %d %d"&L,&R,&C);
 
        if (L == 0 && R == 0 && C == 0) {
            return 0;
        }
 
        for (int i = 0; i < L; i++) {
            for (int j = 0; j < R; j++) {
                scanf("%s", arr[i][j]);
            }
        }
 
        for (int i = 0; i < L; i++) {
            for (int j = 0; j < R; j++) {
                for (int k = 0; k < C; k++) {
                    if (arr[i][j][k] == 'S') {
                        Sl = i;
                        Sr = j;
                        Sc = k;
                    }
                }
            }
        }
 
        queue<node> q;
        int visited[31][31][31= { 0 };
 
        q.push({ Sl,Sr,Sc,0 });
        visited[Sl][Sr][Sc] = 1;
 
        int ans = 0;
 
        while (!q.empty()) {
            const int l = q.front().l;
            const int r = q.front().r;
            const int c = q.front().c;
            const int cnt = q.front().cnt;
            q.pop();
 
            if (arr[l][r][c] == 'E') {
                ans = cnt;
                break;
            }
 
            for (int i = 0; i < 6; i++) {
                const int ll = l + dl[i];
                const int rr = r + dr[i];
                const int cc = c + dc[i];
 
                if (ll >= 0 && ll < L && rr >= 0 && rr < R && cc >= 0 && cc < C) {
                    if((arr[ll][rr][cc] == '.' || arr[ll][rr][cc] == 'E'&& visited[ll][rr][cc] != 1) {
                        visited[ll][rr][cc] = 1;
                        q.push({ ll,rr,cc,cnt + 1 });
                    }
                }
            }
        }
 
        if (ans == 0) {
            printf("Trapped!\n");
        }
        else {
            printf("Escaped in %d minute(s).\n", ans);
        }
    }
}
cs
반응형

+ Recent posts