본문 바로가기
한국으로/알고리즘 문제풀이

[문제풀이] 6593번: 상범 빌딩

by 영킴. 2018. 7. 4.

[문제풀이] 상범 빌딩


문제유형

BFS

Solution

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
#include <stdio.h>
#define MAX 32
#define CIR (32*32*32)
int L, R, C;    //던전 층수, 행 렬,
char map[MAX][MAX][MAX];
char chk[MAX][MAX][MAX];
typedef struct st
{
    int count;
    int x, y, z;
}POS;
POS start;
POS end;
POS q[CIR];
 
void init()
{
    int i, j, k;
    for (i = 0; i < MAX; i++)
    {
        for (j = 0; j < MAX; j++)
        {
            for (k = 0; k < MAX; k++)
            {
                map[i][j][k] = 0;
                chk[i][j][k] = 0;
            }
        }
    }
    start.x = 0; start.y = 0; start.z = 0; start.count = 0;
    end.x = 0end.y = 0end.z = 0end.count = 0;
}
 
void inputData()
{
    int i, j, l;
    scanf("%d %d %d"&L, &R, &C);
    for (l = 0; l < L; l++)
    {
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                char ch = 0;
                scanf(" %c"&ch);
                map[l][i][j] = ch;
                if (ch == 'S')
                {
                    start.x = j;
                    start.y = i;
                    start.z = l;
                    start.count = 0;
                    chk[l][i][j] = 1;
                }
                else if (ch == 'E')
                {
                    end.x = j;
                    end.y = i;
                    end.z = l;
                }
            }
        }
    }
}
 
int bfs()
{
    int k;
    int dx[6= { -101000};
    int dy[6= { 0-10100};
    int dz[6= { 0000-11};
    POS ndata, tdata;
    int wp = 0, rp = 0;
 
    q[wp++= start;
    wp %= CIR;
    //printf("[push] %d %d %d\n", start.x, start.y, start.z);
 
    while (wp != rp)
    {
        tdata = q[rp++];
        rp %= CIR;
 
        for (k = 0; k < 6; k++)
        {
            ndata.x = tdata.x + dx[k];
            ndata.y = tdata.y + dy[k];
            ndata.z = tdata.z + dz[k];
            ndata.count = tdata.count + 1;
 
            if (ndata.z < 0 || ndata.y < 0 || ndata.x < 0continue;
            if (ndata.z >= L || ndata.y >= R || ndata.x >= C) continue;
            if (map[ndata.z][ndata.y][ndata.x] == '#'continue;
            if (chk[ndata.z][ndata.y][ndata.x]) continue;
 
            if (ndata.z == end.z && ndata.y == end.y && ndata.x == end.x)
            {
                return ndata.count;
            }
 
            chk[ndata.z][ndata.y][ndata.x] = 1;
 
            q[wp++= ndata;
            wp %= CIR;
        }
    }
    return 0;
}
 
void solve()
{
    int ret = 0;
    if (ret = bfs())
    {
        printf("Escaped in %d minute(s).\n", ret);
    }
    else
    {
        printf("Trapped!\n");
    }
}
 
int main()
{
    for (;;)
    {
        init();
        inputData();
        if (L == 0 && R == 0 && C == 0break;
        solve();
    }
 
    return 0;
}
 
cs