(C++)数据结构实验二——迷宫问题

BFS算法迷宫

#include <iostream>
#include <string>
//#include <queue>
#include "QueueBottom.h"
#include <fstream>
using namespace std;
#include <iomanip>
#include <Windows.h>

#include <time.h>
clock_t start,finish;
   
   
//改变字体颜色
void setColour(int x){
   
	HANDLE h = GetStdHandle (-11);
	SetConsoleTextAttribute(h,x);
}

//四个方向
int maze[100][100], past_footprint[100][100];
int dx[4] = {
   0, 1, 0, -1};
int dy[4] = {
   1, 0, -1, 0};

//存储最终的路径的数组
int final_array[100][100];

int printmaze[100][100];
int second_printmaze[100][100];

int whole_step;
struct point
{
   
	int x;
	int y;
	int step;
};
queue<point> now_footprint;
queue<point> second_footprint;
void printMaze(int& endx, int& endy, int& startx, int& starty, int& length, int& width, string mapname)
{
   
	ifstream first(mapname, ios::in);
	if (!first.is_open())
	{
   
		cout << "Error: opening file fail" << endl;
		system("pause");
		exit(1);
	}
	while (!first.eof())
	{
   
		first >> length >> width ;
		for (int i = 0; i < length; i++)
			for (int j = 0; j < width; j++)
				first >> maze[i][j];
		first  >> endx >> endy >> startx >> starty;
	}
	// //之后可以改成从txt文件中读取
	// //输入竖向长度x
	// cout << "Please Enter the length of maze:" << endl;
	// cin >> length ;
	
	// //输入横向长度y
	// cout << "Please Enter the width of maze:" << endl;
	// cin >> width ;
	
	// //输入迷宫的形状,1为通路,2为障碍,没有边框
	
	// cout << "Enter the whole maze:" << endl;
	// for (int i = 0; i < length; i++)
	// 	for (int j = 0; j < width; j++)
	// 		cin >> maze[i][j];

	// //输入迷宫的起点	
	// cout << "Please Enter the start of maze" << endl;
	// cin >> endx >> endy ;

	// //输入迷宫的终点
	// cout << "Please Enter the end of maze" << endl;
	// cin >> startx >> starty ;
}
void findRoad(int startx, int starty, int endx, int endy, int length, int width)
{
   
	//BFS
	//队首元素初始化
	point start;
	start.x = startx;
	start.y = starty;
	start.step = 0;
	
	int tag = 1;

	now_footprint.push(start);
	past_footprint[startx][starty] = 1;

	cout << "The Map before function :" << endl;
	cout << " 1 for road; 2 for wall " << endl << endl;

	for(int m = 0; m < length;m++)
	{
   
		for(int n = 0; n < width;n++)
		{
   
			cout << maze[m][n] << "  ";
		}
		cout << endl;
	}
	cout << endl;

	//终点标志
	int flag = 0;
	while(!now_footprint.empty())
	{
   
		//将该队列的首元素的 x y 分别赋予临时的 x y
		int x = now_footprint.front().x;
		int y = now_footprint.front().y;

		//判断该队列的元素是否与终点匹配
		printmaze[x][y] = now_footprint.front().step;
		whole_step = now_footprint.front().step;
		if
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值