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

这篇博客介绍了如何利用BFS算法解决迷宫问题,包括在当前目录下创建txt文件来表示迷宫,BFS算法使用queue作为数据结构,同时提供了QueueBottom.h的相关代码实现。

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;
		
以下是使用 C 语言实现迷宫问题的完整代码: ```c #include<stdio.h> #include<stdlib.h> #include<conio.h> #define MAX_ROW 5 #define MAX_COL 5 int maze[MAX_ROW][MAX_COL] = { {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}, }; void print_maze() { int i, j; for (i = 0; i < MAX_ROW; i++) { for (j = 0; j < MAX_COL; j++) { printf("%d ", maze[i][j]); } printf("\n"); } printf("*********\n"); } typedef struct { int row, col; } element; element stack[512]; int top = -1; void push(element item) { stack[++top] = item; } element pop() { return stack[top--]; } int is_empty() { return top == -1; } void path() { int i, row, col, next_row, next_col; element position; maze[0][0] = 2; top = 0; stack[0].row = stack[0].col = 0; while (!is_empty()) { position = pop(); row = position.row; col = position.col; printf("(%d, %d)\n", row, col); if (row == MAX_ROW - 1 && col == MAX_COL - 1) { printf("Path found!\n"); return; } if (col+1 < MAX_COL && maze[row][col+1] == 0) { // right next_row = row; next_col = col+1; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (row+1 < MAX_ROW && maze[row+1][col] == 0) { // down next_row = row+1; next_col = col; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (col-1 >= 0 && maze[row][col-1] == 0) { // left next_row = row; next_col = col-1; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (row-1 >= 0 && maze[row-1][col] == 0) { // up next_row = row-1; next_col = col; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } print_maze(); } printf("Path not found\n"); } int main() { path(); return 0; } ``` 在上述代码中,我们定义了一个 `maze` 数组来表示迷宫,0 表示可以通过的路,1 表示障碍物,2 表示走过的路程。我们使用栈来记录已经走过的路径,并通过 `push()` 和 `pop()` 操作来实现栈的基本功能。在 `path()` 函数中,我们从起点开始遍历迷宫,如果找到了终点,则输出 "Path found!",否则输出 "Path not found"。 我们可以通过调用 `print_maze()` 函数来输出迷宫的状态,以及调用 `path()` 函数来寻找迷宫的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值