迷宫课设-水

 、问题描述(功能要求):
可以任意定义一个迷宫,用非递归的方法求出走出迷宫的通路,并把路径输出出来。
要求: 存储结构、基本算法(可以使用程序流程图)、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法。
1) 迷宫的存储结构要合理;
2) 应该考虑算法的时间和空间复杂度。
3) 当确定迷宫的规模以及形态以后要把至少一条能走出迷宫的路径输出出来;
4)程序应当满足正确性、可读性、健壮性和高效率及低存储量等目标要求,遵循代码规范,方便调试和阅读。


2 、问题的解决方案:
根据系统功能要求,可以将问题解决分为以下步骤:
( 1 )迷宫可以采用二维数组来存储,迷宫的通路状态可以用不同的字符来表示;
( 2 )根据问题描述,设计算法的实现;
( 3 )建议在解决问题时要采用栈或者队列数据结构;
( 4 )完成算法的各个功能模块;
( 5 )功能调试;
( 6 )完成系统总结报告以及系统使用说明书。
#include <iostream>
#include <cstdio>
#include <cstring> 
#include <algorithm>
using namespace std; 
const int SIZE = 101;

struct Node
{
	int x,y;
	int pre;//父节点 
};
int map[SIZE][SIZE]={0};
int vis[SIZE][SIZE]={0};
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; // 方向向量

Node a[SIZE*SIZE];//模拟队列 
int tail,head;

int n,m;
int startx,starty,p,q;//输入点和输出点;
 
string str;

void Map_Creat()
{
	cout<<"输入地图数据(0为可以通行,1为无法通行)"<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>map[i][j];
		}
	}
	
	cout<<"请输入口和出口"<<endl;
	cin>>startx>>starty>>p>>q;
}

void Map_Opeart()
{
	cout<<"现在开始寻找路径,请稍等。"<<endl;
	
	int tx,ty,flag;
	tail = 1;
	head = 1;
	
	a[tail].x = startx;
	a[tail].y = starty;
	a[tail].pre = 0;
	tail++;
	
	flag=0;//用于标记是否达到目标节点,0表示未达到,1到达
	
	while(head<tail)//队列不为空是循环 
	{
		for(int k=0;k<=3;k++)//枚举四个方向 
		{
			tx=a[head].x+next[k][0];//计算下一个点的坐标 
			ty=a[head].y+next[k][1];
			
			if(tx<0 || tx>=n || ty<0 || ty>=m)//判断是否越界 
			continue;
			
			if(map[tx][ty]==0 && vis[tx][ty]==0)
			{
			 	vis[tx][ty]=1;//标记为已走过
				  
				a[tail].x=tx;
				a[tail].y=ty;
				a[tail].pre = head;//该点从head扩展出,所以他的父节点是head 

				tail++; 
			} 
			  
			if(tx==p && ty==q)
			{
			  	flag=1;
			  	break;
			}
			  
		}
		if(flag==1)
		break;
		
		head++;
	}

	cout<<tail<<endl;
	cout<<"查询结束"<<endl;
	 
}

void Map_Out()
{
	cout<<"开始结果输出"<<endl;
	Node pm,qm;
	pm = qm = a[tail-1];
	
	while(pm.pre != 0)
	{	
		pm = qm;
		cout<<pm.x<<','<<pm.y<<endl;
		qm = a[pm.pre];
		map[pm.x][pm.y] = 2;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cout<<map[i][j];
		}
		cout<<endl;
	}
	cout<<"结果输出完毕"<<endl;
	cout<<endl;
	cout<<"结束程序选择no,其他则继续"<<endl;
} 

void Menu()
{
	cout<<"是否进入迷宫问题解决程序(广搜类解决方案)"<<endl; 
	cout<<"进入选择yes,结束程序选择no"<<endl; 
	while(cin>>str)	
	{
		cout<<endl;
		if(str[0] == 'n') 
		return ;
		cout<<"请输入地图大小(限于100*100)"<<endl;
		
		cin>>n>>m;
		
		Map_Creat();
		Map_Opeart();
		Map_Out();
//		for(int i=0;i<tail;i++)
//		{
//			cout<<i<<" "<<a[i].x<<" "<<a[i].y<<" "<<a[i].pre<<endl;
//		}
	}
}

int main()
{
	Menu();
	cout<<"程序结束"<<endl; 
	return 0;
} 

/*
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

0可走,1不可走
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值