#pragma once
#include <stack>
const size_t N = 10;
void InitMaze(int maze[][N], size_t n)
//void InitMaze(int* maze, size_t n)
{
FILE* fout = fopen("MazeMap.txt", "r");
assert(fout);
for (size_t i = 0; i < n; ++i)
{
for (size_t j = 0; j < n; )
{
char ch = fgetc(fout);
if (ch == '0' || ch == '1')
{
maze[i][j] = ch - '0';
++j;
}
}
}
}
void PrintMaze(int maze[][N], size_t n)
{
for (size_t i = 0; i < n; ++i)
{
for (size_t j = 0; j < n; ++j)
{
cout<<maze[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
struct Pos
{
int _row;
int _col;
};
bool CheckAccess(int maze[][N], size_t n, 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;
}
bool GetMazePath(int maze[][N], size_t n, Pos entry)
{
stack<Pos> path;
path.push(entry);
while (!path.empty())
{
Pos cur = path.top();
// 找到出口
if (cur._row == n-1)
{
return true;
}
Pos next = cur;
maze[cur._row][cur._col] = 2;
// 上
next = cur;
next._row--;
if (CheckAccess(maze, n, next))
{
path.push(next);
continue;
}
// 右
next = cur;
next._col++;
if (CheckAccess(maze, n, next))
{
path.push(next);
continue;
}
// 下
next = cur;
next._row++;
if (CheckAccess(maze, n, next))
{
path.push(next);
continue;
}
// 左
next = cur;
next._col--;
if (CheckAccess(maze, n, next))
{
path.push(next);
continue;
}
Pos top = path.top();
maze[top._row][top._col] = 3;
path.pop();
}
return false;
}
bool CheckAccess(int maze[][N], size_t n, Pos cur, Pos next)
{
if (next._row >= 0 && next._row < n
&& next._col >= 0 && next._col < n)
{
if (maze[next._row][next._col] == 0
|| maze[next._row][next._col] > maze[cur._row][cur._col])
{
return true;
}
}
return false;
}
bool GetMazePathR(int maze[][N], size_t n, Pos cur,
stack<Pos>& shortPath, stack<Pos>& path)
{
path.push(cur);
if (cur._row == n-1)
{
if (shortPath.empty() || path.size() < shortPath.size())
{
shortPath = path;
}
//return true;
}
//maze[cur._row][cur._col] = 2;
Pos next;
// 上
next = cur;
next._row -= 1;
if (CheckAccess(maze, n, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col]+1;
if(GetMazePathR(maze, n, next, shortPath, path))
return true;
}
// 右
next = cur;
next._col += 1;
if (CheckAccess(maze, n, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col]+1;
//if (GetMazePathR(maze, n, next))
if(GetMazePathR(maze, n, next, shortPath, path))
return true;
}
// 下
next = cur;
next._row += 1;
if (CheckAccess(maze, n, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col]+1;
//if (GetMazePathR(maze, n, next))
if(GetMazePathR(maze, n, next, shortPath, path))
return true;
}
// 左
next = cur;
next._col -= 1;
if (CheckAccess(maze, n, cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col]+1;
//if (GetMazePathR(maze, n, next))
if(GetMazePathR(maze, n, next, shortPath, path))
return true;
}
path.pop();
return false;
}
class Maze
{
public:
void GetMazeMap();
void GetShortPath(); //stack<Pos> _path;
void PrintMaze();
protected:
int** _maze;
int _m;
int _n;
stack<Pos> _shortPath;
};
void TestMaze()
{
// 如何动态的开辟一下二维数组?
int maze[N][N];
InitMaze(maze, N);
PrintMaze(maze, N);
stack<Pos> shortPath;
stack<Pos> path;
Pos entry;
entry._row = 2;
entry._col = 0;
maze[2][0] = 2;
//GetMazePath(maze, N, entry);
GetMazePathR(maze, N, entry, shortPath, path);
PrintMaze(maze, N);
}
迷宫
最新推荐文章于 2022-09-28 20:19:18 发布