迷宫可视化:

寻路规则很简单,如果迷宫的东侧可通(没有障碍且没有来过),就往东走,将走过的路径压入栈中;
如果东侧不可通就顺时针改变行走方向,直到找到下一个可通路径;
如果四个方向都不可通 ,将这个路径弹出栈,回到上一步;
最后找到终点结束循环,或者栈空退出,说明无路可走。
代码如下:
#include <iostream>
#include <stdlib.h>
#include <stack>
using namespace std;
typedef struct{
int ord; //序号
int xpos;
int ypos;
//int di; //24↑ 25↓ 27← 26→
}Selem;
Selem element;
char maze[100][101];
void initElem(int step,int x,int y)
{
element.ord = step;
element.xpos = x;
element.ypos = y;
}
bool Status(char mz[][101],Selem elem)
{
int x = elem.xpos,y = elem.ypos;
if(mz[x][y+1] == ‘ ‘)
mz[x][y] = ‘>‘;
else if(mz[x+1][y] == ‘ ‘)
mz[x][y] = ‘|‘;
else if(mz[x][y-1] == ‘ ‘)
mz[x][y] = ‘<‘;
else if(mz[x-1][y] == ‘ ‘)
mz[x][y] = ‘^‘;
else
{
mz[x][y] = ‘$‘;
return false;
}
return true;
}
int main()
{
stack<Selem> path;
int size[2],curpos[2],goal[2],curstep=1;
cout << "迷宫大小(m,n):" ;
cin >> size[0] >> size[1];
cout << "输入迷宫图(#和0组成):" <<endl;
for(int i=0;i<size[0];i++)
for(int j=0;j<size[1];j++)
{
cin >> maze[i][j];
if(maze[i][j] == ‘0‘)
maze[i][j] = ‘ ‘;
}
cout << "输入初始位置:";
cin >> curpos[0] >> curpos[1];
initElem(curstep,curpos[0]-1,curpos[1]-1);
path.push(element);
cout << "输入终点位置:";
cin >> goal[0] >> goal[1];
do
{
if(element.xpos == goal[0]-1 && element.ypos == goal[1]-1)
break;
if(!Status(maze,path.top())) //无通路
path.pop();
else
{
curstep++;
int x = path.top().xpos,y = path.top().ypos;
char c = maze[x][y];
if(c == ‘>‘)
initElem(curstep,x,y+1);
else if(c == ‘|‘)
initElem(curstep,x+1,y);
else if(c == ‘<‘)
initElem(curstep,x,y-1);
else if(c == ‘^‘)
initElem(curstep,x-1,y);
path.push(element);
}
}
while(path.size() != 0); //栈空
maze[goal[0]-1][goal[1]-1] = ‘@‘;
cout << "步数:" << curstep <<endl;
for(int i=0;i<size[0];i++)
{
for(int j=0;j<size[1];j++)
cout << maze[i][j] << " ";
cout <<endl;
}
return 0;
}
因为编译器的控制台输出是GBK编码,输出箭头会乱码,所以用其他符号替代。
输入迷宫样例:
# # # # # # # # # #
# 0 0 # 0 0 0 # 0 #
# 0 0 # 0 0 0 # 0 #
# 0 0 0 0 # # 0 0 #
# 0 # # # 0 0 0 # #
# 0 0 0 # 0 0 0 # #
# 0 # 0 0 0 # 0 0 #
# # # # # 0 # # 0 #
# 0 0 0 0 0 0 0 0 #
# # # # # # # # # #
运行结果:

类似暴力破解,不适用于最短路径问题。
原文:https://www.cnblogs.com/lemon-567/p/14613890.html