minimum number of steps time limit: theMs | Memory Limit:65535KB Difficulty:4describe this there is a maze, there are 00~ and the line8 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,10 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,1To4,1). Enter the first line to enter an integer n (0<n<= -), which indicates that there are n sets of test data, followed by n rows with four integers per line a,b,c,d (0<=a,b,c,d<=8Represents the row, column, and end line of the starting point, respectively. Output output is at least a few steps away. Sample Input23 1 5 73 1 6 7Sample Output A One
The following code is attached to the deep search
Breadth-first search is characterized by a circular way of starting from one point to the surrounding spread wide search is often used to find a single shortest route, or a small-scale path search, it is characterized by "search is the best solution wide search is the operation of the queue, the first enlarged node priority to expand."
1 //The shortest distance from the origin to the end of the range is obtained from each point of the search .2#include <iostream>3#include <queue>4#include <stdio.h>5#include <string.h>6#include <cstring>7 using namespacestd;8 inta[9][9]=9 {Ten 1,1,1,1,1,1,1,1,1, One 1,0,0,1,0,0,1,0,1, A 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, the 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 + }; - structnode + { A intX,y,step;//the coordinates of the node and the distance from the beginning at }; -Queue<node>Q; - intc[4]={0,0,-1,1},b[4]={-1,1,0,0},visite[9][9]; - intBFsintX1,intY1,intX2,inty2) - { - inti,s,t; inNode E={x1,y1,0};//start at this point. -visite[x1][y1]=1; toQ.push (e);//The origin is pressed in. + while(!q.empty ()) - { theE=q.front ();//The first element of the team is taken out * if(e.x==x2&&e.y==y2) $ {Panax Notoginseng Break;//when you jump out, node E is the end point. - } the Q.pop (); + for(i=0;i<4; i++) A { thes=e.x+C[i]; +t=e.y+B[i]; - if(s<=8&&s>=0&&t>=0&&t<=8&&!visite[s][t]&&!A[s][t]) $ { $Node e1={s,t,e.step+1}; - Q.push (E1); -visite[s][t]=1; the } - }Wuyi } the if(Q.empty ()) - return-1; Wu while(!q.empty ()) - Q.pop (); About returnE.step; $ } - intMain () - { - intK,n,x1,x2,y1,y2; Ascanf"%d",&n); + while(n--) the { -memset (visite,0,sizeof(visite)); $scanf"%d%d%d%d",&x1,&y1,&x2,&y2); thek=BFS (x1,y1,x2,y2); theprintf"%d\n", k); the } the return 0; -}
The shortest path problem------to solve problems by deep search and wide search