Maze problem deep search and wide search

Maze solving is a classic problem in experimental psychology. Psychologists drive a mouse into the maze from the entrance of a large box without a top cover. There are many next-doors in the maze, which form many obstacles to the forward direction. The scientists placed a piece of cheese at the only exit of the maze, attracting mice to find their way through the maze to reach the exit.

Input: 1 for barrier, 0 for barrier, 2 for entrance, 3 for exit

Question 1: Find a path and print the path

The idea is: //The idea description draws on the writing method on page 92 of the Data Structure (Second Edition) edited by Mr. Chen Yue, and the following Guangsou also draws on this idea description
(1) The initial entry coordinates and starting direction information Put into the stack
(2) Pop the position information from the stack, set the current position and the current trying direction;
if the stack is empty and the exit is not found, the maze has no solution, and the program exits
(3) At the current position, in order from the current direction Try the accessibility of the remaining directions
If a direction is accessible, store the current position and the next direction on the stack (because when this position is popped, it will start from the stored direction)
If the accessible position is Exit, then exit successfully, and each position in the stack from the bottom of the stack to the top of the stack is the path
. If the passable position is not the exit, set the passable position as the current position, and set the first direction as the current direction; then execute the first three steps 

(4) When the four directions are blocked, go to the second step 

/*
If you go from entry to exit, you need to output from the bottom of the stack to the top of the stack
So here it is used to find the entry from the exit to the last output from the top of the stack to the bottom of the stack
*/
#include<iostream>
#include<stack>
using namespace std;
/*--------coordinates, and start direction to try-------*/
struct Node{
	int x,y,d;
};
int N, dir [4] [2] = {{- 1,0}, {0, -1}, {1,0}, {0,1}};
int maze [100] [100], and [100] [100];
/*-------Pass the entry coordinates and exit coordinates in-------*/
void dfs (int ix, int iy, int ox, int oy);
intmain()
{
	int i,j,k,ix,iy,ox,oy;
	cin>>N;
	for(i=0;i<N;i++){
		for(j=0;j<N;j++){
			cin>>maze[i][j];
			if(maze[i][j]==2){
				ix= i;iy = j;
			}
			else if(maze[i][j]==3){
				ox= i;oy = j;
			}
		}
	}
	dfs(ix,iy,ox,oy);
	return 0;
}
void dfs (int ix, int iy, int ox, int oy)
{
	//stack<Node>s;
	Node tmp;
	stack<Node> s;
	int i,j,k,x,y,nx,ny,d;
	bool find = false;
	tmp.x = ox;tmp.y=oy;tmp.d = 0;
	u[ox][oy] = 1;
	s.push(tmp);
	/*---------When the stack is not empty and no exit is found-------*/
	while(!s.empty() && !find){   //s.empty()==0
		x = s.top().x;y = s.top().y;d = s.top().d;
		s.pop();
		//s.pop(); Node a = s.top();
		while(d<4&&!find){
			nx = x + dir [d] [0];
			ny = y + dir[d][1];
			/*--------if the attempted direction works-------*/
			if((nx>=0 && nx<N) && (ny>=0 && ny<N)&&!u[nx][ny]&& maze[nx][ny]!=1){
				u [nx] [ny] = 1;
				tmp.x = x;
				tmp.y = y;
				tmp.d= d+1;
				s.push(tmp);
				/*---------If it is an exit, but the exit is the direction of the attempt, and it is not pushed onto the stack-------*/
				if(nx==ix&&ny == iy){
					find = true;
					break;
				}
				/*--------If it's not an exit, set the tried position to the current position and the direction to try to 0-------*/
				x = nx;y = ny;d = 0;
			}
			/*---------If this direction doesn't work, try the next direction-------*/
			else{
				d++;
			}
		}
	}
	/*--------If found, output sequentially from the top of the stack to the bottom of the stack-------*/
	if(find){
		
		printf("(%d,%d)\n",ix,iy);
		while(!s.empty()){
			printf("(%d,%d)\n",s.top().x,s.top().y);
			s.pop();
		}
	}
	else{
		printf("NOT FOUND");
	}    
}
Question 2: Find the shortest path, print the path, and display the steps taken

Ideas:
(1) Put the initial entry coordinates into the queue
(2) Get the position information from the head of the queue and set the current position;
if the queue is empty and the exit is not found, the maze has no solution, and the program exits
(3) at the current position , try the adjacent four directions in turn. 
If a certain direction is accessible, the accessible position will be added to the queue, and the previous point of the accessible position is the current position. 
If the accessible position is the exit, it will be successfully exited from the exit coordinates. to find the previous point in turn 

Cycle two or three steps 

/*
At this point, it is not difficult to see that when looking for the previous point from the exit, the output is in reverse order. Here we use the method of finding the entrance from the exit.
*/
#include<iostream>
#include<queue>
using namespace std;
struct Node {
	int x,y;
	Node(){
		
	}
	Node(int i,int j){
		x = i;
		y=j;
	}
} mo [100] [100];
int maze [100] [100], and [100] [100], N, dir [4] [2] = {{0,1}, {1,0}, {0, -1}, {- 1 , 0}};
int bfs (int ix, int iy, int ox, int oy);

intmain()
{
	int i,j,k,ix,iy,ox,oy;
	cin>>N;
	for(i=0;i<N;i++){
		for(j=0;j<N;j++){
			cin>>maze[i][j];
			if(maze[i][j]==2){
				ix= i;iy = j;
			}
			else if(maze[i][j]==3){
				ox= i;oy = j;
			}
		}
	}
	cout<<bfs(ix,iy,ox,oy);
	return 0;	
}
int bfs (int ix, int iy, int ox, int oy)
{
	queue<Node>q;
	struct Node tmp;
	int i,j,k,x,y,nx,ny,cnt = 0;
	bool find = false;
	tmp.x = ox;
	tmp.y = oy;
	q.push(tmp);

	u[ox][oy] = 1;
	while(!q.empty() && !find){  
		tmp = q.front();     
		x = tmp.x;
		y = tmp.y;
		q.pop();
		for(i=0;i<4;i++){
			nx = x + dir [i] [0];
			ny = y + dir[i][1];
			if((nx>=0 && nx<N)&& (ny>=0 && ny<N)&& !u[nx][ny] && maze[nx][ny]!=1){
				u [nx] [ny] = 1;
				q.push(Node(nx,ny));
				mo [nx] [ny] = tmp;
				//printf("(%d,%d)<-(%d,%d)\n",tmp.x,tmp.y,nx,ny);
				if(nx == ix && ny == iy){
					find = true;
					break;
				}
			}
		}
	}
	x = nx;
	y = ny;
        printf("(%d,%d)\n",ix,iy);
	while(!(x == ox && y == oy)){
		cnt++;
	 	printf("(%d,%d)\n",mo[x][y].x,mo[x][y].y);
		int xx = mo[x][y].x;
		int yy = mo[x][y].y;
		x = xx;
		y = yy;
	}
 	return cnt;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324666882&siteId=291194637