迷宫问题

以二维数组表示迷宫,1代表墙壁,0代表通路; (1,1)为入口,(9,9)为出口

{1,1,1,1,1,1,1,1,1,1},        

{1,0,0,1,1,0,0,1,0,1},  
{1,0,0,1,0,0,0,1,0,1},  
{1,0,0,0,0,1,1,0,0,1},  
{1,0,1,1,1,0,0,0,0,1},  
{1,0,0,0,1,0,0,0,0,1},  
{1,0,1,0,0,0,1,0,0,1},  
{1,0,1,1,1,0,1,1,0,1},  

{1,1,0,0,0,0,0,0,0,1},  

{1,1,1,1,1,1,1,1,1,1}};  


一、找出一条从入口到出口的通路

      思路:(1)首先判断所在位置是否为出口,若为出口,则结束。否则转到(2)

                 (2)对该位置相邻的四个位置进行同样的操作(BFS和递归);


#include <iostream>
#include <cstdlib>
#include <vector>
#include <Windows.h>
using namespace std;

int const ROW = 10;
int const COLUMN = 10;

int mp[ROW][COLUMN] = {          //定义一个迷宫,0表示通道,1表示墙  
	{1,1,1,1,1,1,1,1,1,1},       //(1,1)为入口 (9,9)为出口
	{1,0,0,1,1,0,0,1,0,1},  
	{1,0,0,1,0,0,0,1,0,1},  
	{1,0,0,0,0,1,1,0,0,1},  
	{1,0,1,1,1,0,0,0,0,1},  
	{1,0,0,0,1,0,0,0,0,1},  
	{1,0,1,0,0,0,1,0,0,1},  
	{1,0,1,1,1,0,1,1,0,1},  
	{1,1,0,0,0,0,0,0,0,1},  
	{1,1,1,1,1,1,1,1,1,1}};  

int const start_x = 1;
int const start_y = 1;
int const end_x = 8;
int const end_y = 8;

void Draw()
{	
	system("cls");
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COLUMN; ++j)
		{
			if (mp[i][j] == 1)
				cout << "■";
			else if (mp[i][j] == 0)
				cout << "  ";
			else               
				cout << "◇";    //为走过的路径
		}
		cout << endl;
	}
	//Sleep(1000);
}

bool success = false;		//是否已经找到出口

bool visit(int sx, int sy)
{
	mp[sx][sy] = 2;    //将该走过的位置作标记,一是防止再走,二是将其加入路径中

	if (sx == end_x && sy == end_y)  //到达出口 
		success = true;              

	if (!success && mp[sx][sy + 1] == 0)//右
		visit(sx, sy + 1);
	if (!success && mp[sx - 1][sy] == 0)//上
		visit(sx-1,sy);
	if (!success && mp[sx][sy - 1] == 0) //左
		visit(sx, sy - 1);
	if (!success && mp[sx + 1][sy] == 0) //下
		visit(sx + 1, sy);

	if (!success)       //从该位置的四周都无法到达出口
		mp[sx][sy] = 0; //将其从路径中清除

	return success;
}



int main(int argc, char *argv[])
{	
	visit(start_x, start_y);
	Draw();
	return 0;
}



二、从入口到出口的最短路径

 思路:依次找出所有的路径,然后找出一条最短的路径

   具体:如果当前位置已是出口,则结束,否则再从四周找其它的路径。


#include <iostream>
#include <cstdlib>
#include <vector>
#include <Windows.h>
using namespace std;
 
int const ROW = 10;
int const COLUMN = 10;

int mp[ROW][COLUMN] = {          //定义一个迷宫,0表示通道,1表示墙  
	{1,1,1,1,1,1,1,1,1,1},       //(1,1)为入口 (9,9)为出口
	{1,0,0,1,1,0,0,1,0,1},  
	{1,0,0,1,0,0,0,1,0,1},  
	{1,0,0,0,0,1,1,0,0,1},  
	{1,0,1,1,1,0,0,0,0,1},  
	{1,0,0,0,1,0,0,0,0,1},  
	{1,0,1,0,0,0,1,0,0,1},  
	{1,0,1,1,1,0,1,1,0,1},  
	{1,1,0,0,0,0,0,0,0,1},  
	{1,1,1,1,1,1,1,1,1,1}};  

int const start_x = 1;
int const start_y = 1;
int const end_x = 8;
int const end_y = 8;


void Draw()
{	
	system("cls");
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COLUMN; ++j)
		{
			if (mp[i][j] == 1)
				cout << "■";
			else if (mp[i][j] == 0)
				cout << "  ";
			else 
				cout << "◇";
		}
		cout << endl;
	}
	//Sleep(500);
}

vector<pair<int,int> > MinPath;   //保存最短路径
vector<pair<int,int> > Path;      //保存当前路径
 

void visit(int sx, int sy)    
{
	mp[sx][sy] = 2;
	Path.push_back(make_pair(sx,sy)); //将当前位置加入路径中

	if (sx == end_x && sy == end_y)  //当前位置已是出口
	{
		if (MinPath.empty() || Path.size() < MinPath.size()) //当前路径量否比最短路径短
			MinPath.assign(Path.begin(), Path.end());
		mp[sx][sy] = 0;           //清除当前标记
		Path.pop_back();          //将当前位置从路径中除去
		return ;                  //返回上一个位置,再从其它方向探索
	}
	
	if ( mp[sx][sy + 1] == 0) //右
		visit(sx, sy + 1);
	if ( mp[sx - 1][sy] == 0) //左
		visit(sx-1,sy);
	if ( mp[sx][sy - 1] == 0) //上
		visit(sx, sy - 1);
	if ( mp[sx + 1][sy] == 0) //下
		visit(sx + 1, sy);

	mp[sx][sy] = 0;        //清除当前标记
	Path.pop_back();       //将当前位置从路径中除去,返回上一步,在上一步的其它方向继续进行探索
}

int main(int argc, char *argv[])
{	
	visit(start_x, start_y);
	Draw();
	cout << MinPath.size() << endl;
	for (int i = 0; i < MinPath.size(); ++i)
	{
		cout << "(" << MinPath[i].first << "," << MinPath[i].second << ")" << ",";
	}
	return 0;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述: 以一个m*n的长方阵表示迷宫,0和1分别表示迷宫的通路和障碍。设计一个程序,对任意设定的迷宫,求出从入口(0,0)到出口(m-1,n-1)的通路和通路总数,或得出没有通路的结论。例如下图, 0(入口) 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0(出口) 从入口到出口有6条不同的通路。 而下图: 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 从入口到出口则没有通路。 算法设计: 给定一个m*n的长方阵表示迷宫,设计算法输出入口到出口的通路和通路总数,或得出没有通路的结论。 算法提示: 和皇后问题与分书问题类似。可以用二维数组存储迷宫数据,对于迷宫任一位置,均可约定有东、南、西、北四个方向可通。从当前位置a(用(x,y)表示一个位置,假定它是以向右的x轴和向下的y轴组成的平面上的一个点)出发依次尝试四个方向是否有路,若某个方向的位置b可通,则按照同样的方法继续从b出发寻找。若到达出口,则找到一条通路。 数据输入: 由文件input.txt 提供输入数据。第一行是m和n的值,空格分隔,其后共m行。每行有n个数字,数和数之间用空格分隔。 结果输出: 将计算出的所有从入口到出口的通路输出到文件output.txt 。若没有通路,则将0写入文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值