This has a maze, with 0~8 rows and 0~8 columns:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
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
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1
0 represents the road, and 1 represents the wall.
Now enter the coordinates of a road as the starting point, and then enter a road as the coordinates as the end point, ask at least a few steps to reach the end from the starting point?
(Note: One step is to move from one sitting punctuation to the next to the left and right adjacent coordinate points, such as: from (3,1) to (4,1). )
-
-
Input
-
-
The first line enters an integer n (0<n<=100), which indicates that there are n sets of test data;
Then n rows, each row has four integers a,b,c,d (0<=a,b,c,d<=8) representing the row, column, and end line of the starting point, respectively.
Uploaded by
Miao-dong Building
An AC, sure enough! Long time no such feeling!
The code is as follows:
#include <stdio.h> #include <string.h> #include <queue> #define MEM (a) memset (A,0,sizeof (a)) using namespace Std;typedef struct{int x,y;int step;} Node;node Begin,end;int Visit[1000100];int b[2][4]={0,1,0,-1,1,0,-1,0};//forward horizontal ordinate change int hash (node u) {return u.x*100+ u.y;//marks the node}int find (Node U,node v) {return u.x==v.x&&u.y==v.y;} int main () {int i,j;int t;scanf ("%d", &t), while (t--) {mem (visit); int map[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 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, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1};scanf ("%d%d", &BEGIN.X,&BEGIN.Y); BEGIN.STEP=0;SCANF ("%d%d",& END.X,&END.Y); Queue<node>q;q.push (begin); Visit[hash (begin)]=1;map[begin.x][begin.y]=1;while (!q.empty ()) {node U=q.front (); Q.pop (); if (find (U,end)) {printf ("%d\n", u.step); break;} for (int i=0;i<4;++i) {if (map[u.x+b[0][i]][u.y+b[1][i]]==0) {Map[u.x+b[0][i]][u.y+b[1][i]]=1;node v=u;v.x+=b[0][ I];v.y+=b[1][i];v.step++;if (!visit[haSH (v)]) {Visit[hash (v)]=1;q.push (v);}}} while (!q.empty ()) Q.pop ();} return 0;}