POJ2251-Dungeon Master

题目链接:POJ2251

三维搜索,也就是方向变成了6个。6个方向BFS即可

AC代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=35;
char mp[maxn][maxn][maxn];
int l,r,c;
int sl,sr,sc,el,er,ec,ans;
bool vis[maxn][maxn][maxn];
struct node{
    int step;
    int tl,tr,tc;
};

void bfs(int x,int y,int z){
    ans=-1;
    queue<node> q;
    memset(vis,false,sizeof(vis));
    node a;
    a.tl=x;
    a.tr=y;
    a.tc=z;
    a.step=0;
    q.push(a);
    while(!q.empty()){
        node cur=q.front();
      //  printf("%d %d %d\n",cur.tl,cur.tr,cur.tc);
        q.pop();
        if(cur.tl==el&&cur.tr==er&&cur.tc==ec){
            ans=cur.step;
            return;
        }
        if(cur.tl+1<=l&&mp[cur.tl+1][cur.tr][cur.tc]=='.'&&!vis[cur.tl+1][cur.tr][cur.tc]){
            node tmp;
            tmp.tl=cur.tl+1;
            tmp.tr=cur.tr;
            tmp.tc=cur.tc;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl+1][cur.tr][cur.tc]=1;
        }
         if(cur.tr+1<=r&&mp[cur.tl][cur.tr+1][cur.tc]=='.'&&!vis[cur.tl][cur.tr+1][cur.tc]){
            node tmp;
            tmp.tl=cur.tl;
            tmp.tr=cur.tr+1;
            tmp.tc=cur.tc;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl][cur.tr+1][cur.tc]=1;
         }
          if(cur.tc+1<=c&&mp[cur.tl][cur.tr][cur.tc+1]=='.'&&!vis[cur.tl][cur.tr][cur.tc+1]){
            node tmp;
            tmp.tl=cur.tl;
            tmp.tr=cur.tr;
            tmp.tc=cur.tc+1;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl][cur.tr][cur.tc+1]=1;
          }
          if(cur.tc-1>=1&&mp[cur.tl][cur.tr][cur.tc-1]=='.'&&!vis[cur.tl][cur.tr][cur.tc-1]){
            node tmp;
            tmp.tl=cur.tl;
            tmp.tr=cur.tr;
            tmp.tc=cur.tc-1;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl][cur.tr][cur.tc-1]=1;
          }
          if(cur.tr-1>=1&&mp[cur.tl][cur.tr-1][cur.tc]=='.'&&!vis[cur.tl][cur.tr-1][cur.tc]){
            node tmp;
            tmp.tl=cur.tl;
            tmp.tr=cur.tr-1;
            tmp.tc=cur.tc;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl][cur.tr-1][cur.tc]=1;
          }
          if(cur.tl-1>=1&&mp[cur.tl-1][cur.tr][cur.tc]=='.'&&!vis[cur.tl-1][cur.tr][cur.tc]){
            node tmp;
            tmp.tl=cur.tl-1;
            tmp.tr=cur.tr;
            tmp.tc=cur.tc;
            tmp.step=cur.step+1;
            q.push(tmp);
            vis[cur.tl-1][cur.tr][cur.tc]=1;
          }
    }
}

int main(){
    while(scanf("%d%d%d",&l,&r,&c)==3&&l){
        for(int i=1;i<=l;i++){
            for(int j=1;j<=r;j++){
                scanf("%s",mp[i][j]+1);
            }
        }

        ///*
        for(int i=1;i<=l;i++){
            for(int j=1;j<=r;j++){
                for(int k=1;k<=c;k++){
                        if(mp[i][j][k]=='S'){
                            sl=i;
                            sr=j;
                            sc=k;
                        }else if(mp[i][j][k]=='E'){
                            el=i;
                            er=j;
                            ec=k;
                            mp[i][j][k]='.';
                        }
                    //printf("%c",mp[i][j][k]);
                }//puts("");
            }//puts("");
        }
      //  printf("%d %d %d\n",sl,sr,sc);
    //    printf("%d %d %d\n",el,er,ec);
      //  */
        bfs(sl,sr,sc);
        if(ans==-1){
            printf("Trapped!\n");
        }else{
            printf("Escaped in %d minute(s).\n",ans);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值