POJ2251 Dungeon Master kuangbin-搜索入门-B

16 篇文章 0 订阅
12 篇文章 0 订阅

题目链接:POJ2251
题目大意:给你一个三维的空间,给你起始点和终点,求一条从起点到终点的最短的时间也就是最短的路径。

AC代码,简单BFS

/*
2017年7月30日10:20:13
POJ2251 
一直交了好几十发都是 MLE
最后发现 没有打访问标记 = =
AC 
*/

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=30;
char maze[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int l,r,col,sx,sy,sz,ex,ey,ez;
int d[6][3]={{1,0,0},{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{-1,0,0}};
struct node{
    int x,y,z,step;
};

int  bfs(){
    node a,c;
    queue <node > q;
    a.x=sx;
    a.y=sy;
    a.z=sz;
    a.step=1;
    vis[sx][sy][sz]=1;
    q.push(a);
    while(!q.empty()){
        a=q.front();
        q.pop();
        for(int i=0;i<6;i++){
        //  node c;
            c.x=a.x+d[i][0];
            c.y=a.y+d[i][1];
            c.z=a.z+d[i][2];
            if(c.x==ex&&c.y==ey&&c.z==ez)  return a.step;
            if(c.x>=0&&c.x<l &&c.y>=0&&c.y<r &&c.z>=0&&c.z<col&&maze[c.x][c.y][c.z]=='.'&&!vis[c.x][c.y][c.z]){
                c.step=a.step+1;
                vis[c.x][c.y][c.z]=1;
                q.push(c);
            }           
        }           
    }
    return 0;
}


int main(){
    int ans;
    while(~scanf("%d%d%d",&l,&r,&col)&&l){
        memset(maze,'/0',sizeof(maze));
        memset(vis,false,sizeof(vis));
        for(int i=0;i<l;i++){
            for(int j=0;j<r;j++ ){
                scanf("%s",&maze[i][j]);
                for(int k=0;k<col;k++){
                    if(maze[i][j][k]=='S') {
                        sx=i;
                        sy=j;
                        sz=k;
                    }
                    else if(maze[i][j][k]=='E'){
                        ex=i;
                        ey=j;
                        ez=k;
                    }

                    }
                }
            }
        ans=bfs();
        if(ans) printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    } 
    return 0;
}

大家记得BFS一定要打标记,不然队列中会存很多重复的点,导致MLE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值