NYOJ 353

NYOJ 353

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 35;
const int INF = 0xfffffff;

struct Node
{
    int x, y, z;
    int step;
    Node()
    {
        x = y = z = 0;
        step = 0;
    }
};

Node TargetPos;
char Graph[MAXN][MAXN][MAXN];
int MinTime[MAXN][MAXN][MAXN];
int row, col, level;

int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};


bool Is_CanGo(Node CurPos)
{
    if(CurPos.x < 0 || CurPos.x >= level || CurPos.y < 0 || CurPos.y >= row || CurPos.z < 0 || CurPos.z >= col || Graph[CurPos.x][CurPos.y][CurPos.z] == '#')
        return false;
    return true;
}

void BFS(Node S)
{
    queue <Node> Que;
    Que.push(S);
    while(!Que.empty())
    {
        Node Curpos = Que.front();
        Que.pop();

        for(int i = 0; i < 6; ++i)
        {
            Node t;
            t.x = Curpos.x + dir[i][0];
            t.y = Curpos.y + dir[i][1];
            t.z = Curpos.z + dir[i][2];

            if(Is_CanGo(t))
            {
                t.step = Curpos.step + 1;
                if(t.step < MinTime[t.x][t.y][t.z])
                {
                    MinTime[t.x][t.y][t.z] = t.step;
                    Que.push(t);
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d %d %d", &level, &row, &col) && (level + row + col))
    {
        Node StartPos;
        for(int i = 0; i < level; ++i)
        {
            for(int j = 0; j < row; ++j)
            {
                scanf("%s", Graph[i][j]);
                for(int z = 0; z < col; ++z)
                {
                    MinTime[i][j][z] = INF;
                    if(Graph[i][j][z] == 'S')
                        StartPos.x = i, StartPos.y = j, StartPos.z = z;
                    else if(Graph[i][j][z] == 'E')
                        TargetPos.x = i, TargetPos.y = j, TargetPos.z = z;
                }
                getchar();
            }
           if(i != level - 1)
                getchar();
        }
        StartPos.step = 0;
        BFS( StartPos );
        int t;
        t = MinTime[TargetPos.x][TargetPos.y][TargetPos.z];
        if(t != INF)
            printf("Escaped in %d minute(s).\n", t);
        else
            printf("Trapped!\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章