Minimum number of steps time limit: theMs | Memory Limit:65535KB Difficulty:4
-
-
Describe
-
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.
-
-
Output
-
The
-
output is at least a few steps away.
-
-
Sample input
-
-
1 5 1 6 7
-
-
Sample output
-
-
1211
#include <stdio.h> #include <iostream> #include <memory.h> #include <queue>using namespace std; struct point{int x,y,step;}; BOOL 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};int dir[4][2]={1,0,-1,0,0,1,0,-1};int C , d;/** breadth Search */void bfs (int a,int b) {queue<point> q; Point Tmp,p; P.x=a;p.y=b;p.step=0; Q.push (P); BOOL Vis[9][9]; Initializes the memset (vis,0,sizeof (VIS)); while (!q.empty ()) {P=q.front (); Q.pop (); Vis[p.x][p.y]=1; if (P.x==c&&p.y==d) {printf ("%d\n", p.step); Return } for (int i=0;i<4;i++) {tmp.x=p.x+dir[i][0]; TMP.Y=P.Y+DIR[I][1]; tmp.step=p.step+1; if (tmp.x>8| |tmp.x<0| | tmp.y>8| |tmp.y<0) continue; if (Map[tmp.x][tmp.y]==0&&!vis[tmp.x][tmp.y]) Q.push (TMP); }}}int Main () { int n,a,b; cin>>n; while (n--) {cin>>a>>b>>c>>d; BFS (A, b); } return 0;}
Reference Blog: http://blog.csdn.net/hpu_zyh/article/details/10592595
Acm--bfs--nyoj 58--Minimum number of steps