map美化bfs
这是我自认为写得比较好看的一个代码。就是简单的bfs,但是我用了map,使得整体非常好看
https://vjudge.net/contest/372814#problem/B
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) using namespace std; long long h,n,m; long long x[]={1,0,-1,0,0,0}; long long y[]={0,1,0,-1,0,0}; long long z[]={0,0,0,0,-1,1}; char a[32][32][32]; struct node{ long long x; long long y; long long z; bool operator < (const node &o) const{ if(x==o.x){ if(y==o.y) return z<o.z; return y<o.y; } return x<o.x; } }S,E; map<node,long long>ans; map<node,bool>vis; queue<node>q; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } bool inside(node k){ if(k.x<1 || k.x>n) return 0; if(k.y<1 || k.y>m) return 0; if(k.z<1 || k.z>h) return 0; return 1; } void bfs(){ q.push(S); vis[S]=1; while(!q.empty()){ node k=q.front(),temp; q.pop(); For(i,0,5){ temp.x=k.x+x[i]; temp.y=k.y+y[i]; temp.z=k.z+z[i]; if(inside(temp) && !vis[temp] && a[temp.x][temp.y][temp.z]!='#'){ vis[temp]=1; ans[temp]=ans[k]+1; q.push(temp); } } } } void init(){ vis.clear(); ans.clear(); } signed main(){ while(cin>>h>>n>>m&&(n|m|h)){ init(); For(k,1,h) For(i,1,n) For(j,1,m){ cin>>a[i][j][k]; if(a[i][j][k]=='S'){ S.x=i;S.y=j;S.z=k; } if(a[i][j][k]=='E'){ E.x=i;E.y=j;E.z=k; } } bfs(); if(vis[E]) cout<<"Escaped in "<<ans[E]<<" minute(s)."<<endl; else puts("Trapped!"); } return 0; }