1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <string.h> #include <queue> using namespace std; int area[35][35][35][35],vis[35][35][35][35]; int x,y,z,w,sx,sy,sz,sw,ex,ey,ez,ew; struct Node { int x,y,z,w,step; Node(int x,int y,int z,int w,int s):x(x),y(y),z(z),w(w),step(s){};
};
int dir[8][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{-1,0,0,0},{0,-1,0,0},{0,0,-1,0},{0,0,0,-1}};
int check(int dx,int dy,int dz,int dw) { if (dx<0||dx>=x||dy<0||dy>=y||dz<0||dz>=z||dw<0||dw>=w) return 0; else return 1; } int bfs() { Node tmp(sx,sy,sz,sw,0); queue<Node> que; que.push(tmp); vis[tmp.x][tmp.y][tmp.z][tmp.w]=tmp.step; while(!que.empty()) { tmp=que.front(); que.pop(); for (int ijk=0;ijk<8;ijk++) { int dx=tmp.x+dir[ijk][0]; int dy=tmp.y+dir[ijk][1]; int dz=tmp.z+dir[ijk][2]; int dw=tmp.w+dir[ijk][3]; if (check(dx,dy,dz,dw)==0||area[dx][dy][dz][dw]==1) continue; if (dx==ex&&dy==ey&&dz==ez&&dw==ew) return tmp.step+1; if (vis[dx][dy][dz][dw]!=0&&vis[dx][dy][dz][dw]<=tmp.step+1) continue; vis[dx][dy][dz][dw]=tmp.step+1; Node aaa(dx,dy,dz,dw,tmp.step+1); que.push(aaa); }
} return -1; } int main() { while(scanf("%d%d%d%d",&x,&y,&z,&w)!=EOF) { char tmp; memset(area,0,sizeof(area)); memset(vis,0,sizeof(vis)); for (int i=0;i<x;i++) for (int j=0;j<y;j++) for (int k=0;k<z;k++) for (int l=0;l<w;l++) { cin>>tmp; if (tmp=='#') area[i][j][k][l]=1; else if (tmp=='S') { sx=i;sy=j;sz=k;sw=l; } else if (tmp=='E') { ex=i;ey=j;ez=k;ew=l; } } int ans=bfs(); if (ans!=-1) printf("%d\n",ans); else printf("WTF\n"); } return 0; }
|