- 如何将迷宫定义出来,这里我们用的是文件。在mazemap.txt中将迷宫输入好保存,‘1’代表不通,‘0’代表通。然后从文件中读出来,保存在一个二维数组中,打印到屏幕上。
- 迷宫是一种栈的应用。那怎么用栈求解迷宫呢。定义一个空栈,每走一步,将坐标push到栈中,往那个方向走,四个方向依次判断,当此时的四个方向都不通时,栈后进先出,出栈,判断当前其它方向是否通。就这样通就进栈,不通出栈判断其它方向,当栈为空时还没有找到出口,说明迷宫求解失败,没有出口。
//回溯法实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stack>
using namespace std;
#define N 10
void GetMap(int a[][N],size_t n)//定义迷宫直接从文件中读取
{
FILE* fp = fopen("mazemap.txt","r");
if (fp == NULL)
{
cout << "打开文件失败" << endl;
return;
}
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n;)
{
char ch = fgetc(fp);
if (ch == '1' || ch == '0')
{
a[i][j] = ch - '0';
j++;
}
}
}
}
void Print(int a[][N], size_t n)//打印迷宫
{
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
struct pos//位置坐标
{
int _row;
int _col;
};
bool CheckAcess(int a[][N], int n, pos cur)//判断每一步的合法性
{
if ((cur._row >= 0) && (cur._row < n)&&(cur._col >= 0) && (cur._col < n)&&(a[cur._row][cur._col] == 0))
return true;
else
return false;
}
bool GetPath(int a[][N], size_t n,pos entry)
{
stack<pos> mark;//该迷宫采用回溯法,走的每一步都压栈中,当遇到四边都走不通了,回溯,出栈,判断前一步的其它方向是否可以走,直到栈为空时,说明没有找到出口
mark.push(entry);//刚开始将入口压到栈中
a[entry._row][entry._col] = 2;
while (!mark.empty())
{
pos cur = mark.top();
if (cur._row == n - 1)//找到出口
{
return true;
}
pos temp = cur;
//上
cur._row -= 1;
if (CheckAcess(a,n,cur))
{
mark.push(cur);//合法,压栈,走过的标记为2
a[cur._row][cur._col] = 2;
continue;
}
cur = temp;
//下
cur._row += 1;
if (CheckAcess(a,n,cur))
{
mark.push(cur);
a[cur._row][cur._col] = 2;
continue;
}
cur = temp;
//左
cur._col -= 1;
if (CheckAcess(a, n, cur))
{
mark.push(cur);
a[cur._row][cur._col] = 2;
continue;
}
cur = temp;
//右
cur._col += 1;
if (CheckAcess(a, n, cur))
{
mark.push(cur);
a[cur._row][cur._col] = 2;
continue;
}
mark.pop();
}
if (mark.empty())
{
return false;
}
}
迷宫
运行后显示的走过的路径及出路
求解最短路径
求解最短路径,那就需要我们所有路径都找出来,而前面的回溯法它不能找到所有通路,所有这里我们可以采用另一种方法递归实现。每次把当前位置当成起点来判断下个位置的四个方向那个可以通。利用递归找到它的所有通路。然后两个栈,path,shortpath,每次将path和shortpath比较,如果比shortpath小,就把path给shortpath。当有两段路共享移段路时,当有一条走过,数字就会改变,如果都变为2,而当另一条路径走到这是,它就认为这段路是不通的,所以这样是不行的。那我们每次把它的路径加一,只要判断下个点的数字比当前的大,它就可以走。
//最短路径求解
bool CheckAcess(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 GetPathR(int maze[][N], size_t n,pos cur, stack<pos>& path, stack<pos>& shortpath)
{
path.push(cur);
if (cur._row == n - 1)
{
if (shortpath.empty() || path.size() < shortpath.size())
{
shortpath = path;
}
}
pos next = cur;
//上
next._row -= 1;
if (CheckAcess(maze, n,cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col]+1;
if (GetPathR(maze, n, next, path, shortpath))
{
return true;
}
}
//下
next = cur;
next._row += 1;
if (CheckAcess(maze, n,cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetPathR(maze, n, next, path, shortpath))
{
return true;
}
}
//左
next = cur;
next._col -= 1;
if (CheckAcess(maze, n, cur,next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetPathR(maze, n, next, path, shortpath))
{
return true;
}
}
//右
next = cur;
next._col += 1;
if (CheckAcess(maze, n,cur, next))
{
maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
if (GetPathR(maze, n, next, path, shortpath))
{
return true;
}
}
path.pop();
return false;
}
结果