邝斌系列
最短路模板题
看样例就懂了:
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;
}