POJ 2251:Dungeon Master

http://bailian.openjudge.cn/practice/2251/

思路

“3D”的BFS,其实就是多个两个搜索的方向。(上下前后左右)

#include <iostream>
#include <queue>

using namespace std;

int L, R, C, Find, FindStep;
int startx, starty, startz, endx, endy, endz;
char board[33][33][33];
bool vis[33][33][33];

struct node
{
    int x, y, z;
    int step;
    node(){}
    node(int xx, int yy, int zz, int s)
    {
        x = xx;
        y = yy;
        z = zz;
        step = s;
    }
};

int main(int argc, char const *argv[])
{
    //freopen("in.txt", "r", stdin);

    while(cin >> L >> R >> C)
    {
        Find = 0;
        if(L==0 && R==0 && C==0)
            break;
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<R;j++)
            {
                for(int k=0;k<C;k++)
                {
                    cin>>board[i][j][k];
                    vis[i][j][k] = false;
                    if(board[i][j][k] == 'S')
                    {
                        startz = i;
                        startx = j;
                        starty = k;
                    }
                    if(board[i][j][k] == 'E')
                    {
                        endz = i;
                        endx = j;
                        endy = k;
                        board[i][j][k] = '.';
                    }
                }
            }
        }

        queue<node> q;
        q.push(node(startx, starty, startz, 0));
        while(!q.empty())
        {
            node first = q.front();     // 取出队首
            q.pop();

            int x = first.x, y = first.y, z = first.z, step = first.step;

            // cout<<z<<" <<< "<<x<< " <<< "<<y<<" <<< "<<board[z][x][y]<<endl;

            if(z==endz && x==endx && y==endy)
            {
                Find = 1;
                FindStep = first.step;
                break;
            }
            if(z>0 && vis[z-1][x][y]==false && board[z-1][x][y]=='.')
            {
                vis[z-1][x][y] = true;
                q.push(node(x, y, z-1, step+1));
            }
            if(z<L-1 && vis[z+1][x][y]==false && board[z+1][x][y]=='.')
            {
                vis[z+1][x][y] = true;
                q.push(node(x, y, z+1, step+1));
            }
            if(x>0 && vis[z][x-1][y]==false && board[z][x-1][y]=='.')
            {
                vis[z][x-1][y] = true;
                q.push(node(x-1, y, z, step+1));
            }
            if(y>0 && vis[z][x][y-1]==false && board[z][x][y-1]=='.')
            {
                vis[z][x][y-1] = true;
                q.push(node(x, y-1, z, step+1));
            }
            if(y<C-1 && vis[z][x][y+1]==false && board[z][x][y+1]=='.')
            {
                vis[z][x][y+1] = true;
                q.push(node(x, y+1, z, step+1));
            }
            if(x<R-1 && vis[z][x+1][y]==false && board[z][x+1][y]=='.')
            {
                vis[z][x+1][y] = true;
                q.push(node(x+1, y, z, step+1));
            }

        }

        if(Find)
            printf("Escaped in %d minute(s).\n", FindStep);
        else
            printf("Trapped!\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值