以二维数组表示迷宫,1代表墙壁,0代表通路; (1,1)为入口,(9,9)为出口
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
一、找出一条从入口到出口的通路
思路:(1)首先判断所在位置是否为出口,若为出口,则结束。否则转到(2)
(2)对该位置相邻的四个位置进行同样的操作(BFS和递归);
#include <iostream>
#include <cstdlib>
#include <vector>
#include <Windows.h>
using namespace std;
int const ROW = 10;
int const COLUMN = 10;
int mp[ROW][COLUMN] = { //定义一个迷宫,0表示通道,1表示墙
{1,1,1,1,1,1,1,1,1,1}, //(1,1)为入口 (9,9)为出口
{1,0,0,1,1,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
int const start_x = 1;
int const start_y = 1;
int const end_x = 8;
int const end_y = 8;
void Draw()
{
system("cls");
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COLUMN; ++j)
{
if (mp[i][j] == 1)
cout << "■";
else if (mp[i][j] == 0)
cout << " ";
else
cout << "◇"; //为走过的路径
}
cout << endl;
}
//Sleep(1000);
}
bool success = false; //是否已经找到出口
bool visit(int sx, int sy)
{
mp[sx][sy] = 2; //将该走过的位置作标记,一是防止再走,二是将其加入路径中
if (sx == end_x && sy == end_y) //到达出口
success = true;
if (!success && mp[sx][sy + 1] == 0)//右
visit(sx, sy + 1);
if (!success && mp[sx - 1][sy] == 0)//上
visit(sx-1,sy);
if (!success && mp[sx][sy - 1] == 0) //左
visit(sx, sy - 1);
if (!success && mp[sx + 1][sy] == 0) //下
visit(sx + 1, sy);
if (!success) //从该位置的四周都无法到达出口
mp[sx][sy] = 0; //将其从路径中清除
return success;
}
int main(int argc, char *argv[])
{
visit(start_x, start_y);
Draw();
return 0;
}
二、从入口到出口的最短路径
思路:依次找出所有的路径,然后找出一条最短的路径
具体:如果当前位置已是出口,则结束,否则再从四周找其它的路径。
#include <iostream>
#include <cstdlib>
#include <vector>
#include <Windows.h>
using namespace std;
int const ROW = 10;
int const COLUMN = 10;
int mp[ROW][COLUMN] = { //定义一个迷宫,0表示通道,1表示墙
{1,1,1,1,1,1,1,1,1,1}, //(1,1)为入口 (9,9)为出口
{1,0,0,1,1,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
int const start_x = 1;
int const start_y = 1;
int const end_x = 8;
int const end_y = 8;
void Draw()
{
system("cls");
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COLUMN; ++j)
{
if (mp[i][j] == 1)
cout << "■";
else if (mp[i][j] == 0)
cout << " ";
else
cout << "◇";
}
cout << endl;
}
//Sleep(500);
}
vector<pair<int,int> > MinPath; //保存最短路径
vector<pair<int,int> > Path; //保存当前路径
void visit(int sx, int sy)
{
mp[sx][sy] = 2;
Path.push_back(make_pair(sx,sy)); //将当前位置加入路径中
if (sx == end_x && sy == end_y) //当前位置已是出口
{
if (MinPath.empty() || Path.size() < MinPath.size()) //当前路径量否比最短路径短
MinPath.assign(Path.begin(), Path.end());
mp[sx][sy] = 0; //清除当前标记
Path.pop_back(); //将当前位置从路径中除去
return ; //返回上一个位置,再从其它方向探索
}
if ( mp[sx][sy + 1] == 0) //右
visit(sx, sy + 1);
if ( mp[sx - 1][sy] == 0) //左
visit(sx-1,sy);
if ( mp[sx][sy - 1] == 0) //上
visit(sx, sy - 1);
if ( mp[sx + 1][sy] == 0) //下
visit(sx + 1, sy);
mp[sx][sy] = 0; //清除当前标记
Path.pop_back(); //将当前位置从路径中除去,返回上一步,在上一步的其它方向继续进行探索
}
int main(int argc, char *argv[])
{
visit(start_x, start_y);
Draw();
cout << MinPath.size() << endl;
for (int i = 0; i < MinPath.size(); ++i)
{
cout << "(" << MinPath[i].first << "," << MinPath[i].second << ")" << ",";
}
return 0;
}