http://acm.hdu.edu.cn/showproblem.php?pid=1240
began not to carefully read the question, understand the discovery is a bare bfs, pay attention to the coordinates are three-dimensional, and then each can expand out of 6 directions.
The first dimension is represented on the third level. The latter two dimensions represent rows and columns.
1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 struct Point6 {7 intX,y,z,step;8 BOOL operator< (ConstPoint a)Const9 {Ten returnStep>A.step; One } A }; - Point t,e; - intN; the Charmaze[ the][ the][ the]; - intused[ the][ the][ the]; - intdir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; - + voidBFS () - { +memset (Used,0,sizeof(used)); APriority_queue<point>que; at Que.push (t); -used[t.z][t.x][t.y]=1; - while(!que.empty ()) - { -Point s=que.top (); Que.pop (); - //printf ("%d%d%d%d\n", s.z,s.x,s.y,s.step); in if(S.X==E.X&&S.Y==E.Y&&S.Z==E.Z) {printf ("%d%d\n", N,s.step);return;} - for(intI=0;i<6; i++) to { +t.x=s.x+dir[i][0],t.y=s.y+dir[i][1],t.z=s.z+dir[i][2]; - if(t.x>=0&&t.x<n&&t.y>=0&&t.y<n&&t.z>=0&&t.z<n&&maze[t.z][t.x][t.y]!='X'&&!Used[t.z][t.x][t.y]) the { *t.step=s.step+1; $used[t.z][t.x][t.y]=1;Panax Notoginseng Que.push (t); - } the } + } Aprintf"NO route\n"); the } + intMain () - { $ //freopen ("A.txt", "R", stdin); $ Chars[Ten]; - while(~SCANF ("%s", s)) - { thescanf"%d",&n); - for(intI=0; i<n;i++)Wuyi for(intj=0; j<n;j++) thescanf"%s", Maze[i][j]); -scanf"%d%d%d",&t.y,&t.x,&t.z); Wut.step=0; -scanf"%d%d%d",&e.y,&e.x,&e.z); Aboutscanf"%s", s); $ BFS (); - } - return 0; -}
hdu-1240 Nightmare && hdu-1253 Victory Grand Escape (BFS)