1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 6 intd[9][9];//Mark whether this point accesses and records distances7 intmaze[9][9]={8 1,1,1,1,1,1,1,1,1,9 1,0,0,1,0,0,1,0,1,Ten 1,0,0,1,1,0,0,0,1, One 1,0,1,0,1,1,0,1,1, A 1,0,0,0,0,1,0,0,1, - 1,1,0,1,0,1,0,0,1, - 1,1,0,1,0,1,0,0,1, the 1,1,0,1,0,0,0,0,1, - 1,1,1,1,1,1,1,1,1}; - Const intinf=1<<Ten; -typedef pair<int,int>P; + intN,sx,sy,gx,gy; - intdx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; + A intBFS () at { -memset (D,0,sizeof(d)); -Queue<p>que; - for(intI=0;i<9; i++) - for(intj=0;j<9; j + +) -d[i][j]=INF; in Que.push (P (Sx,sy)); -d[sx][sy]=0; to + while(!que.empty ()) - { theP p=Que.front (); * Que.pop (); $ if(p.first==gx&&p.second==gy)Panax Notoginseng Break; - the for(intI=0;i<4; i++) + { A intNx=p.first+dx[i],ny=p.second+Dy[i]; the + if(0<=nx&&nx<=8&&0<=ny&&ny<=8&&maze[nx][ny]==0&&d[nx][ny]==inf)//whether the current position can be joined - { $ Que.push (P (Nx,ny)); $d[nx][ny]=d[p.first][p.second]+1; - } - } the } - returnD[gx][gy];Wuyi } the - intMain () Wu { -scanf"%d",&n); About while(n--) $ { -scanf"%d%d%d%d",&sx,&sy,&gx,&gy); -printf"%d\n", BFS ()); - } A return 0; +}
View Code
Challenge Programming Maze Shortest Path template
Minimum number of steps Nyoj