#include <iostream>
#include <ctime>
#define ROW 6
#define COL 6
int a[ROW][COL] = {
1, 1, 1, 1, 0, 1,
1, 1, 1, 0, 0, 1,
0, 1, 1, 1, 1, 1,
0, 1, 1, 0, 1, 0,
0, 0, 0, 0, 1, 1,
0, 1, 1, 1, 0, 1};//1:通路; 0:障碍
struct Stack
{
int pos[ROW * COL][2];
int (*top)[2];
int (*base)[2];
};
void InitStack(Stack & stack)
{
stack.base = stack.pos;
stack.top = stack.pos;
}
bool Push(Stack & stack, int pos[2])
{
if (stack.top - stack.base == ROW * COL)
{
return false;
}
(*stack.top)[0] = pos[0];
(*stack.top)[1] = pos[1];
stack.top++;
return true;
}
bool Pop(Stack & stack)
{
if(stack.top - stack.base == 0)
{
return false;
}
stack.top--;
return true;
}
void DisplayStack(const Stack & stack)
{
int (*ptr)[2];
for (ptr = stack.base; ptr < stack.top; ptr++)
{
std::cout << (*ptr)[0] << ", "<< (*ptr)[1] << std::endl;
}
}
bool accessable(int maze[][COL], int pos[2], bool dir[])
{
int up[2] = {pos[0] - 1, pos[1]};
int right[2] = {pos[0], pos[1] + 1};
int down[2] = {pos[0] + 1, pos[1]};
int left[2] = {pos[0], pos[1] - 1};
dir[0] = up[0] >= 0 && maze[up[0]][up[1]] != 0;
dir[1] = right[1] < COL && maze[right[0]][right[1]] != 0;
dir[2] = down[0] < ROW && maze[down[0]][down[1]] != 0;
dir[3] = left[1] >= 0 && maze[left[0]][left[1]] != 0;
return dir[0] || dir[1] || dir[2] || dir[3];
}
//求解路径栈(非递归)
void FindPath(Stack & stack, int entry[2], int exit[2], int (*m)[COL])
{
int maze[ROW][COL];
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
maze[i][j] = m[i][j];
}
}//复制迷宫矩阵,后续将走不通的格子也用0标记
int currPos[2] = {entry[0], entry[1]};//currPos记录当前点行列
Push(stack, currPos);
maze[currPos[0]][currPos[1]] = 0;//将走过的格子设为障碍
while (!(currPos[0] == exit[0] && currPos[1] == exit[1])/*不是出口*/ && stack.base != stack.top/*栈不空(栈空说明连第一个都出栈了,表示全是死路)*/)
{
bool dirAccess[4] = {};//记录上、右、下、左是否可行
if (accessable(maze, currPos, dirAccess))
{
if (dirAccess[0])//上可行
{
int up[2] = {currPos[0] - 1, currPos[1]};
Push(stack, up);
maze[currPos[0]][currPos[1]] = 0;//经过后设为障碍
currPos[0] = up[0];
currPos[1] = up[1];
continue;
}
else if (dirAccess[1])//右可行
{
int right[2] = {currPos[0], currPos[1] + 1};
Push(stack, right);
maze[currPos[0]][currPos[1]] = 0;
currPos[0] = right[0];
currPos[1] = right[1];
continue;
}
else if (dirAccess[2])//下可行
{
int down[2] = {currPos[0] + 1, currPos[1]};
Push(stack, down);
maze[currPos[0]][currPos[1]] = 0;
currPos[0] = down[0];
currPos[1] = down[1];
continue;
}
else if (dirAccess[3])//左可行
{
int left[2] = {currPos[0], currPos[1] - 1};
Push(stack, left);
maze[currPos[0]][currPos[1]] = 0;
currPos[0] = left[0];
currPos[1] = left[1];
continue;
}
}
else
{
Pop(stack);
maze[currPos[0]][currPos[1]] = 0;
currPos[0] = (*(stack.top - 1))[0];
currPos[1] = (*(stack.top - 1))[1];
}
}
}
int main()
{
int entry[2] = {0, 0};//入口
int exit[2] = {5, 5};//出口
Stack stack;
InitStack(stack);
FindPath(stack, entry, exit, a);
for(int (*ptr)[2] = stack.base; ptr != stack.top; ptr++)
{
std::cout << (*ptr)[0] << ", " << (*ptr)[1] << std::endl;
}
std::cout << std::endl;
char map[ROW][COL];
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
map[i][j] = a[i][j] == 1 ? ' ' : '0' ;
}
}
for (int (*ptr)[2] = stack.base; ptr != stack.top; ptr++)
{
map[(*ptr)[0]][ptr[0][1]] = 'P';
}
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
std::cout << map[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}
这种方法只能找到路径,但不是最短路径,并且在通道宽处可能绕圈
最简单逻辑迷宫求解
最新推荐文章于 2021-09-25 15:47:48 发布