Dungeon Master POJ - 2251 BFS

邝斌系列
最短路模板题
看样例就懂了:

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

多加两个方向,向上和向下即可。

#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iostream>

using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
#define mem(s,t) memset(s,t,sizeof(s))
#define D(v) cout<<#v<<" "<<v<<endl
#define inf 0x3f3f3f3f
#define pb push_back

//#define LOCAL
const int mod=1e9+7;
const int MAXN =500+10;

int l,r,c;
int dx[]={0,0,1,-1,0,0};
int dy[]={0,0,0,0,-1,1};
int dz[]={1,-1,0,0,0,0};
char m[50][50][50];
int vis[50][50][50];
struct node{
    int x,y,z,ret;
    bool operator == (const node& a){
        return a.x==x && a.y==y && a.z==z;
    }
};
queue<node> que;
node s,t;
int bfs(){
    que.push(s);
    vis[s.x][s.y][s.z]=1;
    while(!que.empty()){
        node a=que.front();
        //D(a.ret);
        que.pop();
        if(a.x==t.x && a.y==t.y && a.z==t.z) return a.ret;
        for(int i=0;i<6;i++){
            node temp;
            temp.x=a.x+dx[i];
            temp.y=a.y+dy[i];
            temp.z=a.z+dz[i];
            temp.ret=a.ret+1;
            if(temp.x<l && temp.x>=0 && temp.y>=0 && temp.y<r && temp.z<c && temp.z>=0
                && m[temp.x][temp.y][temp.z]!='#' && !vis[temp.x][temp.y][temp.z]){
                que.push(temp);
                vis[temp.x][temp.y][temp.z]=1;
            }
        }
    }
    return -1;
}
int main() {
    while(~scanf("%d%d%d",&l,&r,&c) && l+r+c){
        mem(vis,0);
        while(!que.empty()) que.pop();
        for(int i=0;i<l;i++){
            for(int j=0;j<r;j++){
                scanf("%s",m[i][j]);
                for(int k=0;k<c;k++){
                    if(m[i][j][k]=='S')
                        s.x=i,s.y=j,s.z=k,s.ret=0;
                    else if(m[i][j][k]=='E')
                        t.x=i,t.y=j,t.z=k,t.ret=0;
                }
            }
        }
        int ans=bfs();
        if(ans==-1){
            puts("Trapped!");
        }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、付费专栏及课程。

余额充值