C++实现迷宫问题

发布时间:2020-05-31 15:13:32 作者:CLOWN5
来源:网络 阅读:1310

新建一个.txt文档来存储迷宫,输入n*n的迷宫矩阵并保存起来,如下图

C++实现迷宫问题

//Stack.h

#pragma once

template <class T>
class stack
{

public:
	stack()        //构造函数
		:_arr(NULL)
		, _top(0)
		, _capacity(0)
	{}
	~stack()          //析构函数
	{
		if (_arr)
		{
			delete[] _arr;
		}
	}
public:
	void Push(const T& x)         //插入
	{
		_CheckCapacity();
		_arr[_top++] = x;
	}
	void Pop()          //删除
	{
		assert(_top > 0);
		--_top;
	}
	size_t Size()         //大小
	{
		return _top;
	}
	bool Empty()         //判断栈是否为空
	{
		//return _top == 0;
		if (_top <= 0)
		{
			return true;
		}
		else
			return false;
	}
	T& Top()        //获取栈顶元素
	{
		return _arr[_top - 1];
	}

protected:
	void _CheckCapacity()
	{
		if (_arr == NULL)
		{
			_capacity = 5;
			_arr = new T[_capacity];
			return;
		}
		else if (_top == _capacity)
		{
			_capacity *= 2;
			T* tmp = new T[_capacity];
			for (size_t i = 0;i < _top;++i)
			{
				tmp[i] = _arr[i];
			}
			delete[] _arr;
			_arr = tmp;
		}
	}
protected:
	T* _arr;
	size_t _top;
	size_t _capacity;
};

//Maze.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

#include <assert.h>
#include "Stack.h"

struct Pos
{
	int _row;
	int _col;
};

//函数声明
void GetMaze(int* a, int n);
bool SearchMazePath(int *a, int n, Pos entrance, stack<Pos>& paths);
bool CheckIsAccess(int* a, int n, const Pos& next);

void GetMaze(int* a, int n)   //将迷宫存到一个一维数组a里
{
	assert(a);
	FILE* fout = fopen("E:\\MazeMap.txt", "r");    //以读的方式读取迷宫矩阵
	assert(fout);
	for (int i = 0;i < n;++i)
	{
		for (int j = 0;j < n;)
		{
			char ch = fgetc(fout);
			if (ch == '1' || ch == '0')    //矩阵中‘0’为通路
			{
				a[i*n + j] = ch - '0';
				cout << a[i*n + j]<<" ";
				++j;
			}
		}
		cout << endl;
	}
	cout << endl;
}

bool CheckIsAccess(int* a, int n, const Pos& next)
{
	int row = next._row;
	int col = next._col;
	if (row >= 0 && row < n && col >= 0 && col < n && a[row*n + col] == 0)
	{
		return  true;
	}
	else
	{
		return false;
	}
}

//寻找通路,并用栈来存储
bool SearchMazePath(int *a, int n, Pos entrance, stack<Pos>& paths)  
{
	assert(a);
	paths.Push(entrance);
	bool isfirst = true;
	while (!paths.Empty())
	{
		Pos cur = paths.Top();
		a[cur._row*n + cur._col] = 7;   //将通路标记为7
		
		if (isfirst == false || cur._row == n - 1 || cur._col == n - 1 
			|| cur._row == 0 || cur._row == 0 )    //如果找到矩阵的四个边就表示找到通路
		{
			return true;
		}
		Pos next = cur;

		//判断当前元素的上路是否为通路,即是否为‘0’
		next._row--;
		if (CheckIsAccess(a, n, next))
		{
			paths.Push(next);
			continue;
		}

		//判断当前元素的下路是否为通路
		next = cur;
		next._row++;
		if (CheckIsAccess(a, n, next))
		{
			paths.Push(next);
			continue;
		}

		//判断当前元素的左路是否为通路
		next = cur;
		next._col--;
		if (CheckIsAccess(a, n, next))
		{
			paths.Push(next);
			continue;
		}

		//判断当前元素的右路是否为通路
		next = cur;
		next._col++;
		if (CheckIsAccess(a, n, next))
		{
			paths.Push(next);
			continue;
		}

		paths.Pop();
		isfirst = false;
	}
	return false;
}

//打印迷宫矩阵
void PrintMaze(int* a, int n)
{
	for (int i = 0;i < n;++i)
	{
		for (int j = 0;j < n;++j)
		{
			cout << a[i*n + j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}


int main()
{
	int size = 5;
	int mazemap[25] = { 0 };
	Pos entrance;
	entrance._row = 1;
	entrance._col = 0;
	stack<Pos> path;

	GetMaze(mazemap, size);
	if (SearchMazePath(mazemap, size, entrance, path))
	{
		PrintMaze(mazemap, size);
	}

	system("pause");
	return 0;
}


推荐阅读:
  1. 迷宫问题代码
  2. C++解迷宫问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

迷宫 矩阵 栈 c+ c++实现

上一篇:vSphere使用vCenter server进行虚拟主机管理

下一篇:C#通过WMI读取MAC地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》