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
Your own search is not very good, reference code address: 54 Floor
http://acm.nyist.net/JudgeOnline/talking.php?pid=58
#include <iostream> #include <queue> #include <string.h>using namespace Std;int num[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};int visit [9] [9];struct point{int x;int y;int Step;}; void BFs (int startx,int starty,int endx,int endy) {queue<point>q; Point Now,next;int x[4]={0,1,0,-1}; int Y[4]={1,0,-1,0};int k,exit=0; Now.step=0;now.x=startx;now.y=starty; Q.push (now); while (!q.empty ()) {Next=q.front (); Q.pop (); if (Next.x==endx &&next.y==endy) {exit=1; break;} for (k=0;k<4;k++) {now.x=next.x+x[k];now.y=next.y+y[k];if (!visit[now.x][now.y] &&now.x>=0 && Now.x<9 &&now.y>=0 &&now.y<9 &&num[now.x][now.y]==0) {visit[now.x][now.y]= 1;now.step=next.step+1;q.push (now);}}} if (exit) Cout<<next.step<<endl;elsecout<<-1<<endl;} int main () {int A,b,c,d,t;cin>>t;while (t--) {cin>>a>>b>>c>>d; memset (visit,0,sizeof (visit)); BFS (A,B,C,D); } return 0;}