Branch and bound method for solving maze problems

Problem Description

Start from the entrance and explore forward in a certain direction. If you can pass it (have not passed it yet), you can find a place where you canarrive, then reach the new point, otherwise try the next direction; if there is no path in all directions of the point, then follow the original pathReturn to previous point, change the direction and continue to test until all possible paths have been explored, or a path is found, or there is no way to go and you return to the entry point.
The "previous point" returned to is the one that has just been visited. It has the characteristics of "last in, first out". It is necessary to use a stack to save the subscript of each point that can be reached and the direction of progress from that point.

Please add image description

Maze problem data structure

Use the two-dimensional array maze[M][N] to represent the maze. The part in the red box in the picture is the maze, and the mazeThe surrounding values ​​are all 1 (i.e. no connection). If designed in this way, all points in the maze will evolve into a certain point in the middle of the maze, which can ensure that no matter which pointThere are 4 test directions.
Insert image description here

Directional heuristic representation

typedef struct{
    
    
	//x,y方向的向量
	int incX,intcY; 
} Direction;
Direction direct[4];
//从某点(x,y)按某一方向v(0<=v<=3)达到新的点(line,col)的坐标
line=x+direct[v].incX;
col=y+direct[v].intcY;

Insert image description here

Organization of elements in the stack

typedef struct{
    
    
	int x,y;//当前访问的迷宫格子的横纵坐标 
	int direction;//当前方向 
}Box; 

Prevent reaching a certain point repeatedly

Option One:

  • In addition, set the flag array flag[m][n], with all its elements initialized to 0. When a certain point (i, j) is reached, its corresponding flag[i][j] is set to 1, and the next time it is tested, position, you cannot select it.
    Option II

  • When a certain point (i, j) is reached, the corresponding maze[i][j] is set to -1. The value of other points that have not been reached can only be 1 or 0, which can be distinguished from the points that have not been reached.

Code

Insert image description here

#include<stdio.h>
#define MAXQ 100
#define MAZN 10
//初始化迷宫 
int n=8;
char Maze[MAZN][MAZN]={
    
    
	{
    
    '0','1','1','1','1','1','1','1'},
	{
    
    '0','0','0','0','0','1','1','1'},
	{
    
    '1','0','1','1','0','0','0','1'},
	{
    
    '1','0','1','1','0','1','1','0'},
	{
    
    '1','0','1','1','1','1','1','1'},
	{
    
    '1','0','1','1','0','0','0','1'},
	{
    
    '1','0','0','0','0','1','0','0'},
	{
    
    '1','1','1','1','1','1','1','0'}
};
	//方向结构体 
	struct Direction{
    
    
		int incX,incY;
	};
	 Direction dir[4]={
    
    {
    
    0,-1},{
    
    1,0},{
    
    0,1},{
    
    -1,0}}; 
	//当前位置,探索的方向 
	struct Position{
    
    
		int x,y;
		int pre;
	};
	Position qu[MAXQ];
	//队头和队尾 
	int front=-1,rear=-1;
	//输出迷宫路径 
	void disppath(int front)
	{
    
    
		int i,j;
		//之前走过设置为-1的都还原为0 
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(Maze[i][j]=='-1')
					Maze[i][j]='0';
		int k=front;
		//能走通的路径设置为空 
		while(k!=-1)
		{
    
    
			Maze[qu[k].x][qu[k].y]=' ';
			k=qu[k].pre;
		}
		//遍历出该路径 
		for(i=0;i<n;i++)
		{
    
    
			printf(" ");
			for(int j=0;j<n;j++)
				printf("%c",Maze[i][j]);
			printf("\n");
		}
		//输出路径具体位置
		for(i=0;i<n;i++)
		{
    
    
			for(int j=0;j<n;j++)
			if(Maze[i][j]==' ')
			printf("(%d,%d)-->",i,j); 
		 } 
		 printf("End");
	}
	//广度优先遍历 
	void BFS(int x,int y)
	{
    
    
		Position p,p1,p2;
		p.x=x;p.y=y;p.pre=-1;
		//走过的位置为-1,下次不能再走了 
		Maze[p.x][p.y]='-1';
		//队尾++ 
		rear++;
		//更新当前解 
		qu[rear]=p;
		while(front!=rear)
		{
    
    
			//队头++ 
			front++;
			p1=qu[front];
			//找到一条通路,输出路径 
			if(p1.x==n-1&&p1.y==n-1)
			{
    
    
				disppath(front);
				return;
			}
			//试探上,右,下,左四个方向(0,-1)(1,0)(0,1)(-1,0) 
			for(int k=0;k<4;k++)
			{
    
    
				p2.x=p1.x+dir[k].incX;
				p2.y=p1.y+dir[k].incY;
				//满足,不能为负,不能超出迷宫,要是通路 
				if(p2.x>=0&&p2.y>=0&&p2.x<n&&p2.y<n&&Maze[p2.x][p2.y]=='0')
				{
    
    
					//该位置走过为-1 
					Maze[p2.x][p2.y]='-1';
					//更新方向 
					p2.pre=front;
					rear++;
					//入队 
					qu[rear]=p2;
				}
			}
		}
	}
	int main()
	{
    
    
		int x=0,y=0;
		printf("迷宫路径:\n");
		BFS(x,y);
		return 0;
	}
迷宫路径:
  1111111
   111111
 1 111111
 1 111110
 1 111111
 1 11   1
 1    1
 1111111
 
(0,0)-->(1,0)-->(1,1)-->(2,1)-->(3,1)-->(4,1)-->(5,1)-->(5,4)-->(5,5)-->(5,6)-->(6,1)-->(6,2)-->(6,3)-->(6,4)-->(6,6)-->(6,7)-->(7,7)-->End

Guess you like

Origin blog.csdn.net/qq_52108058/article/details/134238259