POJ 2251题解

传送门:http://poj.org/problem?id=2251

三维的走迷宫,实际上跟二维差不多,就是把坐标放进队列里面时候编码会卡住,其他的话跟二维没啥区别

#include <iostream>
#include <cstring>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;
const int N = 30;
int maps[N][N][N];
int fa[N][N][N];
int dis[N][N][N];
int d[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int queues[N*N*N];
int su,eu;
int L,R,C;
int bfs()
{
    int fronts = 0,rear = 1;
    int z1 = su/900,x1 = (su%900)/30,y1 = (su%900)%30;
    fa[z1][x1][y1] = 0;queues[fronts] = su;
    dis[z1][x1][y1] = 1;
    while(rear>fronts){
        int u = queues[fronts++];
        if(u == eu){
            return dis[eu/900][(eu%900)/30][(eu%900)%30];
        }
        int z = u/900,x = (u%900)/30,y = (u%900)%30;
        for(int i = 0;i<6;i++){
            int newx = x + d[i][0];
            int newy = y + d[i][1];
            int newz = z + d[i][2];
            if(newz >= 0 && newz <L && newx >= 0 && newx <R && newy >= 0 && newy <C &&
                !maps[newz][newx][newy] && !dis[newz][newx][newy]){

                queues[rear++] = newz*900 + newx*30 + newy;
                fa[newz][newx][newy] = u;
                dis[newz][newx][newy] = dis[z][x][y] + 1;
            }
        }
    }
    return -1;
}
int main()
{
    char s;
    char word[N];
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&L,&R,&C) == 3){
            if(!L&&!R&&!C)break;
            s = getchar();
        memset(dis,0,sizeof(dis));
        for(int i = 0;i<L;i++){
            for(int j = 0;j<R;j++){
                cin >> word;
                for(int k = 0;k<C;k++){
                    if(word[k] == '#')maps[i][j][k] = 1;
                    else if(word[k] == '.')maps[i][j][k] = 0;
                    else if(word[k] == 'S'){su = i*900 + j*30 + k;}
                    else if(word[k] == 'E'){maps[i][j][k] = 0;eu = i*900 + j*30 + k;}///编码存放
                }
            }
            //s = getchar();
        }
        int bu = bfs();
        if(bu<0)cout << "Trapped!" << endl;
        else printf("Escaped in %d minute(s).\n",bu-1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值