POJ2251

#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>

using namespace std;

int maze[40][40][40];
int flag = 0;
int L, R, C;
int res = 0;
int sx, sy, sz, ex, ey, ez;
int d[6][3] = {{0,  1,  0},
               {0,  -1, 0},
               {1,  0,  0},
               {-1, 0,  0},
               {0,  0,  1},
               {0,  0,  -1}
};

struct pos {
    int x, y, z;
    int level;
};

queue<pos> p;

int solve(pos in) {
    if (in.x == ex && in.y == ey && in.z == ez) {
        res = in.level;
        flag = 1;
        return 1;
    }
    for (int i = 0; i < 6; i++) {
        pos temp;
        temp.x = in.x + d[i][0];
        temp.y = in.y + d[i][1];
        temp.z = in.z + d[i][2];
        if (temp.x >= 0 && temp.x < L && temp.y >= 0 && temp.y < R && temp.z >= 0 && temp.z < C &&
            maze[temp.x][temp.y][temp.z] == 0) {
            temp.level = in.level + 1;
            p.push(temp);
            maze[temp.x][temp.y][temp.z] = 1;
        }
    }
}

int main() {
    while (cin >> C >> L >> R) {
        if (L == 0 && R == 0 && C == 0)
            break;
        flag = 0;
        for (int i = 0; i < C; i++) {
            for (int j = 0; j < L; j++) {
                for (int k = 0; k < R; k++) {
                    char temp;
                    cin >> temp;
                    if (temp == 'S') {
                        maze[j][k][i] = 1;
                        sx = j;
                        sy = k;
                        sz = i;
                    } else if (temp == '.') {
                        maze[j][k][i] = 0;
                    } else if (temp == 'E') {
                        maze[j][k][i] = 0;
                        ex = j;
                        ey = k;
                        ez = i;
                    } else if (temp == '#')
                        maze[j][k][i] = 1;
                }
            }
        }

        pos start;
        start.x = sx;
        start.y = sy;
        start.z = sz;
        start.level = 0;
        p.push(start);
        while (!p.empty()) {
            pos in = p.front();
            solve(in);
            p.pop();
        }
        if (flag == 1) {
            cout << "Escaped in " << res << " minute(s)." << endl;
        } else
            cout << "Trapped!" << endl;
        while (!p.empty())
            p.pop();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章