迷宫是个益智的小游戏,但是就算是个小游戏也会有外挂吧,那么就来写一个小小的迷宫来玩玩吧(虽然是程序自己走的。。)但是这个不重要,重要的是,我不用烧脑玩迷宫啊~
//面向对象实现迷宫
#include<stack>
struct Pos
{
int _row; //行
int _col; //列
};
template<size_t N>
class Maze
{
public:
//构造函数初始化 二维数组传参问题,必须传递列数
//第一种方式传参
/*Maze(int maze[][N])
{
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N; j++)
{
_maze[i][j] = maze[i][j];
}
}
}*/
Maze(int* maze)
{
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < N; j++)
{
_maze[i][j] = maze[i*N+j];
}
}
}
void Print()
{
for (size_t i = 0; i < N; ++i)
{
for (size_t j = 0; j < N; j++)
{
printf("%d ", _maze[i][j]);
}
cout << endl;
}
cout << endl;
}
bool GetPath(Pos entry,stack<Pos>& path)
{
Pos cur = entry;
path.push(cur);
while (!path.empty())
{
//进行探测
//入口
//next
//回溯的上一个位置
//Pos next = cur;
Pos cur = path.top();
_maze[cur._row][cur._col] = 2;
//检查是否已经到出口
if (cur._row == N - 1){
return true;
}
Pos next = cur;
//上下左右探测
next._row -= 1; //上
if (CheckAccess(next))
{
//cur = next;
path.push(next);
continue;
}
next = cur;
next._row += 1; //下
if (CheckAccess(next))
{
//cur = next;
path.push(next);
continue;
}
next = cur;
next._col -= 1; //左
if (CheckAccess(next))
{
//cur = next;
path.push(next);
continue;
}
next = cur;
next._col += 1; //右
if (CheckAccess(next))
{
//cur = next;
path.push(next);
continue;
}
//回溯
//走完一条路没有找到出口返回则标记为3
Pos back = path.top();
_maze[back._row][back._col] = 3;
path.pop();
}
//走到这里表明没有找到出路
return false;
}
//递归实现找路径
void GetPathR(Pos entry, stack<Pos>& path, stack<Pos>& shortPath)
{
if (!path.empty())
{
Pos prev = path.top();
_maze[entry._row][entry._col] = _maze[prev._row][prev._col] + 1;
}
path.push(entry);
if (entry._row == N - 1)
{
//return true;
if (shortPath.empty()||path.size() < shortPath.size())
{
shortPath = path;
}
}
Pos next = entry;
//上 如何让它找到每条出路,不返回,直接递归
next._row -= 1;
if (CheckAccess(entry,next))
{
//if(GetPathR(next, path))
// return true;
GetPathR(next, path,shortPath);
}
//下
next = entry;
next._row += 1;
if (CheckAccess(entry,next))
{
//if (GetPathR(next, path))
// return true;
GetPathR(next, path,shortPath);
}
//左
next = entry;
next._col -= 1;
if (CheckAccess(entry,next))
{
//if (GetPathR(next, path))
//return true;
GetPathR(next, path,shortPath);
}
//右
next = entry;
next._col += 1;
if (CheckAccess(entry,next))
{
//if (GetPathR(next, path))
// return true;
GetPathR(next, path,shortPath);
}
path.pop();
//return false;
}
bool CheckAccess(Pos cur, Pos next)
{
//检查边界
//是否是通路
//是否自己没走过,但是别人走过
if ((next._row >= 0 && next._row < N && next._col >= 0 && next._col < N)
&&(_maze[next._row][next._col]==0
|| _maze[next._row][next._col]>_maze[cur._row][cur._col]))
{
return true;
}
else
{
return false;
}
}
//重载
bool CheckAccess(Pos pos)
{
if (pos._row < N&&pos._row >= 0
&&pos._col < N&&pos._col >= 0
&&_maze[pos._row][pos._col] == 0)
{
return true;
}
return false;
}
private:
int _maze[N][N];
};
void TestMaze()
{
const size_t N = 10;
int maze[N][N] = {
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{2,0,0,1,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,1,1},
{1,1,0,0,0,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1}
};
//如何动态开辟一个二维数组
//1、直接开辟一维数组:M*N+j
//2、指针
//第一种传参方式
//Maze<N> mz(maze);
Maze<N> mz((int*)maze);
mz.Print();
Pos entry;
stack<Pos> path;
stack<Pos> shortPath;
entry._row = 2;
entry._col = 0;
//cout << "是否找到出口?"<<mz.GetPathR(entry, path) << endl;
mz.GetPathR(entry, path,shortPath);
mz.Print();
//cout << shortPath.size() << endl;
while (!shortPath.empty())
{
Pos pos = shortPath.top();
printf("(%d,%d)\n", pos._row, pos._col);
shortPath.pop();
}
cout << endl;
//mz.Print();
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<assert.h>
#include"Maze.h"
int main()
{
TestMaze();
system("pause");
return 0;
}
整个迷宫就完成啦,界面化实现我还在慢慢摸索中~~