Maze Shortest Path DFS and BFS code analysis

Source: Internet
Author: User

DFS uses recursive step-by-step heuristics to select the shortest path in all paths

Code:

0 is the road, 1 is the wall 
#include <iostream>
#include <algorithm>
using namespace std;
int x1,y1,x2,y2;//Start coordinate end coordinate
int c=999999;//step 
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int mg[5][5]={0,0,1,1,1,
              0,0,1,1,1,
              0,1,0,1,1,
              1,0,1,1,1,
              0,0,0,0,0
			 };
void Dfs (int x,int y,int t)
{
	int i,xx,yy;
	if (x==x2&&y==y2)//arrive at the end of 
	{
		c=min (c,t);//Take the minimum value 
		return;
	}
	mg[x][y]=1;//will walk through the road set 1, lest next time think this is a road, and come back for 
	(i=0;i<4;i++)
	{
		xx=x+dir[i][0];
		yy=y+dir[i][1];//generates a new direction coordinate 
		if (xx<0| | xx>=5| | yy<0| | yy>=5| | Mg[xx][yy]==1)//Beyond the map, or for the wall, you have to change direction to go 
		 continue;
		DFS (xx,yy,t+1);///Step +1, with Xx,yy as the new coordinates, to select the next direction and generate a new XXYY 
		mg[xx][yy]=0;//This step is when the end is reached, or there is a XXYY coordinates can not go up or down, To restore the road you just walked (previously set to a wall) 
	}
}
int main ()
{
	cin>>x1>>y1>>x2>>y2;
	DFS (x1,y1,0);
	cout<<c;
	return 0;
}


BFS Use the queue one layer at a time to take the shortest

Code:

 #include <iostream> #include <algorithm> #include <queue> using namespace std;
The struct St {int x,y,c=0;//column coordinate xy,c is the step number};
Queue<struct st> xz;//Create a queue xy int dir[4][2]={-1,0,1,0,0,-1,0,1}; int mg[5][5]={0,0,1,1,1, 0,0,0,1,1, 0,1,0,1,1, 0,0,0,1,1, 0,0,0,0,
0};
	int BFs (struct St S,struct St e) {struct St T;
	int i;
	Xz.push (s);//First the information of the starting coordinates is enqueued mg[s.x][s.y]=1; while (!xz.empty ())//queue is empty, the description arrives at the end, complete {s=xz.front ();//Get the team header element Xz.pop ();//delete the queue is empty if (S.X==E.X&&S.Y==E.Y)/
		/reach the end, return step c return S.C;
			for (i=0;i<4;i++) {t.x=s.x+dir[i][0];
			T.Y=S.Y+DIR[I][1]; if (t.x<0| | t.x>=5| | t.y<0| | t.y>=5| |
			 MG[T.X][T.Y])//Can not go continue;  t.c=s.c+1;
	Number of steps plus 1 mg[t.x][t.y]=1;//traversed by the road 1 Xz.push (t);//The new coordinates and steps of the information T Queue}}} int main () {struct St S,e;
	cin>>s.x>>s.y>>e.x>>e.y;
	COUT<<BFS (s,e) <<endl;
return 0; }


Print out Path

Input 0 for road, 1 for Wall, 5*5 map//from upper left corner to bottom right #include <iostream> #include <algorithm> #include <queue> using
namespace Std;
int mg[5][5];
int dir[4][2]={-1,0,1,0,0,-1,0,1}; struct node {int x,y,c;}
	jl[5][5];//Record path int bfs () {int i;
	Queue<struct node> Q;
	struct node S;
	struct node T;
	s.x=s.y=s.c=0;
	Mg[s.x][s.y]=1;
	Q.push (s); while (!
		Q.empty ()) {S=q.front ();
		Q.pop ();
		if (s.x==4&&s.y==4)//reach the end {return S.C;
			} for (i=0;i<4;i++) {t.x=s.x+dir[i][0];
			T.Y=S.Y+DIR[I][1];
				if (t.x>=0&&t.x<5&&t.y>=0&&t.y<5&&mg[t.x][t.y]==0) {t.c=s.c+1;
				jl[t.x][t.y].x=s.x;//here the JL is the record path jl[t.x][t.y].y=s.y;
				Mg[t.x][t.y]=1;
			Q.push (t);
}}} return 0; 
	 	} void print (int xx,int yy)//recursive print path {if (xx==0&&yy==0) {cout<<xx<< "" <<yy<<endl;
	 return;
	 } print (JL[XX][YY].X,JL[XX][YY].Y);
cout<<xx<< "" <<yy<<endl;} int main () {int i,j;
	for (i=0;i<5;i++) for (j=0;j<5;j++) cin>>mg[i][j];
	COUT&LT;&LT;BFS () <<endl;
print (bis); }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.