【面试题】迷宫问题---广度优先搜索-----队列

//迷宫问题---广度优先搜索-----队列
#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std;
#define MAX_ROW 5
#define MAX_COL 5

struct point
{
	int row;
	int col;
};

queue<point> s;

int maze[MAX_ROW][MAX_COL] = 
{
	0,0,0,0,0,
	0,0,0,1,0,
	0,1,0,0,0,
	0,0,0,0,0,
	0,0,0,1,0
};

void print_maze()
{
	for(int i=0; i<MAX_ROW; ++i)
	{
		for(int j=0; j<MAX_COL; ++j)
		{
			printf("%d ",maze[i][j]);
		}
		printf("\n");
	}
	printf("************************\n");
}

struct point predecessor[MAX_ROW][MAX_COL] = 
{
	{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
	{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
	{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
	{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
	{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}
};

void visit(int row,int col,struct point pre)
{
	point visit_point = {row,col};
	maze[row][col] = 2;
	predecessor[row][col] = pre;
	s.push(visit_point);
}

int main()
{
	point p = {0,0};
	maze[p.row][p.col] = 2;
	s.push(p);
	while(!s.empty())
	{
		p = s.front();
		s.pop();
		//抵达目的地
		if(p.row == MAX_ROW-1 && p.col == MAX_COL-1)
		{
			break;
		}

		//向下移动
		if(p.row+1 < MAX_ROW && maze[p.row+1][p.col]==0)
		{
			visit(p.row+1,p.col,p);
		}

		//向右移动
		if(p.col+1 < MAX_COL && maze[p.row][p.col+1]==0)
		{
			visit(p.row,p.col+1,p);
		}

		//向上移动
		if(p.col-1 >= 0 && maze[p.row][p.col-1]==0)
		{
			visit(p.row,p.col-1,p);
		}
		//向左移动
		if(p.row-1>=0 && maze[p.row-1][p.col]==0)
		{
			visit(p.row-1,p.col,p);
		}
		print_maze();
	}
	if(p.row == MAX_ROW-1 && p.col == MAX_COL-1)
	{
		printf("(%d,%d)\n",p.row,p.col);
		while(predecessor[p.row][p.col].row != -1)
		{
			p = predecessor[p.row][p.col];
			printf("(%d,%d)\n",p.row,p.col);
		}
	}
	else
	{
		cout <<"NO Path"<<endl;
	}
	return 0;
}

 

运行结果:

 

2 2 0 0 0
2 0 0 1 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 0 0 0
2 2 0 1 0
2 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 0 1 0
2 1 0 0 0
0 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 0 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 0 0
2 2 2 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 0 0 0
2 0 0 0 0
0 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 0 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2 0
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 0 0 1 0
************************
2 2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 0 0 0
2 2 0 1 0
************************
2 2 2 2 2
2 2 2 1 0
2 1 2 0 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2 2
2 2 2 1 0
2 1 2 2 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 0 0
2 2 0 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 0 0
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 0
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 0
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 2
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 2
2 2 2 1 0
************************
2 2 2 2 2
2 2 2 1 2
2 1 2 2 2
2 2 2 2 2
2 2 2 1 2
************************
(4,4)
(3,4)
(3,3)
(3,2)
(3,1)
(3,0)
(2,0)
(1,0)
(0,0)
请按任意键继续. . .

 

 

 

使用一个0-1矩阵来模拟一个迷宫,1表示障碍物,0表示通道。访问过的用2表示

要求从左上角入口进入迷宫,从右下角的出口走出迷宫,本题模拟出了寻找出口的过程,以后我会改进此程序,使其输出所有可行的路径

有些题目还要求只能向右或者向下走,那么只需要撤去本题中的向左和向上两种条件选择就行了。

 

 

 

 

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/bxyill/article/details/7799809
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞