迷宫——c++实现

本文用c++实现了一个简易的迷宫,用户给迷宫一个入口,程序可应用递归或者循环找到当前的下一个步,如果能走通则将当前道路赋值为2,走不通则为3。

此迷宫有两个特点:1.地图数据保存文件中。2、地图空间动态创建出来

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;


#define MAX_ROW 10
#define MAX_COL 10
#include<fstream>
#include<string>
#include<stack>
struct Seat
{
	int _x;
	int _y;
	Seat(int x , int y )
		: _x(x)
		, _y(y)
	{}
};
//1、地图数据保存文件中
//2、地图空间动态创建出来
class Maze
{
public:

	Maze(const string& fileName)
	{
		string strBuff;
		string strData;
		fstream fin(fileName);
		getline(fin, strBuff);
		strData = strBuff.substr(0, strBuff.find_first_of(','));
		 ROW = atoi(strData.c_str());//按照const char*返回出来

		strData = strBuff.substr(strBuff.find_first_of(',') + 1);
		 COL = atoi(strData.c_str());

		_map = new int*[ROW];
		for (int idx = 0; idx < ROW; idx++)
		{
			_map[idx] = new int[COL];
		}

		int rowIdx = 0, colIdx = 0;
		while (!fin.eof())
		{
			getline(fin, strData);
			char* start = (char*)strData.c_str();
			while (*start)
			{
				if (*start >= '0'&&*start <= '9')
				{
					_map[rowIdx][colIdx++] = *start - '0';
				}
				start++;
			}
			rowIdx += 1;
			colIdx = 0;
		}
		fin.close();
	}
	Maze(int map[][MAX_COL], int row)
	{
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < MAX_COL; ++j)
			{
				_map[i][j] = map[i][j];
			}
		}
	}

	bool IsPass(const Seat& s)
	{
		if (s._x < 0 || s._y < 0 || s._y >= MAX_COL)
			return true;

		if (1 == _map[s._x][s._y])
			return true;

		return false;
	}

	bool PassMaze(const Seat& Entry)//递归
	{
		if (Entry._x < 0 || Entry._y < 0 || Entry._y >= MAX_COL)
			return true;

		if (IsPass(Entry))
		{
			_map[Entry._x][Entry._y] = 2;
			// 超前
			Seat front(Entry._x - 1, Entry._y);
			if (PassMaze(front))
			{
				return true;
			}
			
			// 超左
			Seat left(Entry._x, Entry._y - 1);
			if (PassMaze(left))
			{
				return true;
			}
			// 超右边
			Seat right(Entry._x, Entry._y + 1);
			if (PassMaze(right))
			{
				return true;
			}
			_map[Entry._x][Entry._y] = 3;
		}

		return false;
	}



	bool PassMaze(std::stack<Seat>&s, const Seat& Entry)//循环
	{
		if (!IsPass(Entry))
			return false;
		
			s.push(Entry);
			while (!s.empty())
			{
				Seat Cur = s.top();
				if (Cur._x < 0 || Cur._y < 0 || Cur._y >= MAX_COL)
				{
					s.pop();
					return true;
				}
					_map[Cur._x][Cur._y] = 2;
				//前

				Seat Front(Cur._x - 1, Cur._y);
				
				if (IsPass(Front))
				{
					s.push(Front);
					continue;
				}
				//左
				Seat left (Cur._x , Cur._y-1);
				if (IsPass(left))
				{
					s.push(left);
					continue;
				}
				//右
				Seat right(Cur._x, Cur._y + 1);
				if (IsPass(right))
				{
					s.push(right);
					continue;
				}
				_map[Cur._x][Cur._y] = 3;
				s.pop();
			}
		
		return false;
	}


	void PrintMap()
	{
		for (int i = 0; i < MAX_ROW; ++i)
		{
			for (int j = 0; j < MAX_COL; ++j)
			{
				cout << _map[i][j] << " ";
			}
			cout << endl;
		}
	}
	~Maze()
	{
		/*for (int idx = 0; idx < ROW; ++idx)
		{
			int j = 0;
			for (j = 0; j < COL; j++)
			{
				delete _map[] ;
			}
		}*/
		for (int i = 0; i < ROW; ++i)
		{
			delete[] _map[i];
		}
		delete[] _map;
	}
private:
	int **_map;
	int ROW, COL;
};

void FunTest1()//递归调用
{
	int mapArr[MAX_ROW][MAX_COL] = {
		{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 1, 1, 1, 1, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 } };
	Maze maze("map.txt");
	//Maze maze(mapArr, MAX_ROW);
	maze.PrintMap();
	cout << endl;
	stack<int>s;
	Seat s1(9, 6);
	maze.PassMaze(s1);
	cout << endl;
	maze.PrintMap();
}

void FunTest2()//循环
{
	int mapArr[MAX_ROW][MAX_COL] = {
		{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 1, 1, 1, 1, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 } };
	Maze maze("map.txt");
	//Maze maze(mapArr, MAX_ROW);
	maze.PrintMap();
	cout << endl;
	stack<Seat>s;
	Seat s1(9, 6);
	maze.PassMaze(s, s1);
	cout << endl;
	maze.PrintMap();
}

int main()
{
	FunTest1();
	system("pause");
	return 0;
}

程序结果:


#include<iostream> #include<windows.h> #include "conio.h" using namespace std; const int m=10 ; //迷宫行数 const int n=15 ; //迷宫列数 struct migong //结构体迷宫,,,pre为前驱,,,xy 为当前目标 { int x,y; int pre; }sq[500]; //最大成员500个,,迷宫大小,,,,, int mg[m+2][n+2]; //迷宫数组 外带边2个 ,, int zx[8+1],zy[8+1]; //迷宫方向数组 九宫格,,,他在中间,,,所以8个方向 void printlj(int rear) //打印迷宫路径 { int i; i=rear; //保存当前节点前驱,,为了在,,向前试探失败时,返回,,换一个方向继续 do { cout<<sq[i].x<<sq[i].y; //输出当前,,X,y值,,其实就是地址。。。。 i=sq[i].pre; // 保存这个节点的前驱 }while(i!=0); //i!=0表示通路 } void mglj() //球迷宫最短路径 { int i,j,x,y,v,front,rear,found; sq[1].x=sq[1].y=1;sq[1].pre=0; //从(1,1)开始搜索 。。。。左上角开始 found=0; //初始化。 front=rear=1; //初始化 mg[1][1]=-1; //开始节点就不需要来回遍历了,,,不设为-1,,一样可以算,,对于CPU来说无所谓 while((front<=rear)&&(!found)) //found初始化,,等于0 ,,,,这里是一个BUG,,,因为找到了,,他就退出了,,,很多时候不止一条路径 { x=sq[front].x; // y=sq[front].y; //引入临时变量x,y,,,保存当前,,,因为下面要进行,,,查找遍历,,,为了能够回到当前节点,,在无法试探的情况下 for(v=1;v<=8;v++) //循环扫描8个方向 {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值