1 minute read

코드

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define X first
#define Y second

int dx[6] = { 1, 0, -1, 0, 0, 0 };
int dy[6] = { 0, 1, 0, -1, 0, 0 };
int dz[6] = { 0, 0, 0, 0, 1, -1 };
int vis[31][31][31] = { 0 };
string board[31][31];

class cord {
public:
    int x, y, z;
    cord(int _z, int _y, int _x) : x(_x), y(_y), z(_z) {}
};

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);

    while (1) {
        int l, r, c;
        cin >> l >> r >> c;
        if (!l && !r && !c)
            return 0;

        memset(vis, 0, sizeof(vis));
        queue<cord> q;
        cord* ac = new cord(0, 0, 0);

        for (int i = 0; i < l; i++) {
            for (int j = 0; j < r; j++) {
                cin >> board[i][j];
                for (int k = 0; k < c; k++) {
                    if (board[i][j][k] == 'S') {
                        q.push(cord(i, j, k));
                        vis[i][j][k] = 1;
                    }
                    if (board[i][j][k] == 'E') {
                        ac->z = i;
                        ac->y = j;
                        ac->x = k;
                    }
                }
            }
        }
        while (!q.empty()) {
            auto cur = q.front();
            q.pop();
            for (int dir = 0; dir < 6; dir++) {
                int nx = cur.x + dx[dir];
                int ny = cur.y + dy[dir];
                int nz = cur.z + dz[dir];
                if (nx < 0 || ny < 0 || nz < 0 || nx >= c || ny >= r || nz >= l) continue;
                if (board[nz][ny][nx] == '#' || vis[nz][ny][nx] > 0) continue;
                q.push(cord(nz, ny, nx));
                vis[nz][ny][nx] = vis[cur.z][cur.y][cur.x] + 1;
            }
        }
        if (vis[ac->z][ac->y][ac->x] == 0)
            cout << "Trapped!\n";
        else
            cout << "Escaped in " << vis[ac->z][ac->y][ac->x] - 1 << " minute(s).\n";
    }
    return 0;
}

단순한 3차원 bfs입니다.

Tags:

Categories:

Updated:

Leave a comment