(图 BFS)走迷宫

时间:2024-07-24 20:07:26

题目:

  给一个迷宫,求出从起点到终点的路径。迷宫 src.txt 文件内容如下,第一行是迷宫的行列数,后面行是迷宫,1表示可行走,0表示不可以通过,起点是最左上角,终点是最右下角:


解析:

  其实就是图的广度优先遍历。

代码及运行结果:

 #include <iostream>
#include <queue>
#include <fstream>
#include <string>
#include <iomanip> using std::cout;
using std::endl;
using std::cin;
using std::string; class Maze
{
public:
Maze(string file);
~Maze();
void showMaze(void) const;
void getPath(void);
void calc(void);
void showPreNode(void) const;
void showPath(void) const; private:
int m_rows;
int m_colomns;
bool** m_pData; //用bool装迷宫数据 int** m_preNode; //记录路径,也就是前一个节点
bool** m_flag; //标志是否访问过
enum { INVALID = - }; struct Coord //定义一个坐标
{
int x;
int y; bool operator ==(const Coord& rhs)
{
if (this->x == rhs.x && this->y == rhs.y)
return true;
return false;
}
}; void initeAboutCalc(void);
Coord getAssignCoord(const Coord& thisCoord, int num) const;
void showPathPre(int x, int y) const;
}; Maze::Maze(string file)
{
std::ifstream fin(file);
fin >> m_rows >> m_colomns; //分配数据区
m_pData = new bool*[m_rows];
for (int i = ; i < m_rows; i++)
m_pData[i] = new bool[m_colomns]; //读取数据
for (int i = ; i < m_rows; i++)
{
for (int j = ; j < m_colomns; j++)
fin >> m_pData[i][j];
} //初始化其他
initeAboutCalc();
} void Maze::initeAboutCalc(void)
{
m_preNode = new int*[m_rows];
m_flag = new bool*[m_rows]; for (int i = ; i < m_rows; i++)
{
m_preNode[i] = new int[m_colomns];
memset(m_preNode[i], INVALID, m_colomns * sizeof(int)); m_flag[i] = new bool[m_colomns];
memset(m_flag[i], false, m_colomns * sizeof(bool));
}
} Maze::~Maze()
{
for (int i = ; i < m_rows; i++)
delete[] m_pData[i]; delete[] m_pData;
} void Maze::showMaze(void) const
{
for (int i = ; i < m_rows; i++)
{
for (int j = ; j < m_colomns; j++)
cout << m_pData[i][j] << " "; cout << endl;
}
} void Maze::calc(void)
{
const Coord START = { , };
const Coord END = { m_rows - , m_colomns - };
const Coord ERROR_COORD = { -, - }; std::queue<Coord> que; //二维数组下标放到队列里面去
m_flag[START.x][START.y] = true;
que.push(START); //起点 while (true)
{
Coord thisNode = que.front();
que.pop();
if (thisNode == END)
break; //对于这一点的上下左右个点
for (int i = ; i < ; i++)
{
Coord coordTemp = getAssignCoord(thisNode, i);
if (coordTemp == ERROR_COORD || == m_pData[coordTemp.x][coordTemp.y]) //没有这点或者这点是墙
continue; if (!m_flag[coordTemp.x][coordTemp.y]) //没有访问过
{
m_flag[coordTemp.x][coordTemp.y] = true;
m_preNode[coordTemp.x][coordTemp.y] = thisNode.x * m_colomns + thisNode.y; //注意这里用的不是坐标了,但也可唯一确定一个点
que.push(coordTemp);
}
}
}
} Maze::Coord Maze::getAssignCoord(const Coord& thisCoord, int num) const
{
Coord res = { -, - }; //不合法的
if (num < || num >= )
return res; switch (num)
{
case :
{
if (!thisCoord.x)
return res;
res.x = thisCoord.x - ;
res.y = thisCoord.y;
}
case :
{
if (thisCoord.y == m_colomns - )
return res;
res.x = thisCoord.x;
res.y = thisCoord.y +;
}
case :
{
if (thisCoord.x == m_rows - )
return res;
res.x = thisCoord.x + ;
res.y = thisCoord.y;
}
case :
{
if (!thisCoord.y)
return res;
res.x = thisCoord.x;
res.y = thisCoord.y - ;
}
}
return res;
} void Maze::showPreNode(void) const
{
for (int i = ; i < m_rows; i++)
{
for (int j = ; j < m_colomns; j++)
cout << std::setw() << std::left << m_preNode[i][j]; cout << endl;
}
} void Maze::showPath(void) const
{
showPathPre(m_rows - , m_colomns - );
} void Maze::showPathPre(int x, int y) const
{
if ( == x && == y)
{
cout << "(" << x << ", " << y << ")" << endl;
}
else
{
showPathPre(m_preNode[x][y] / m_colomns, m_preNode[x][y] % m_colomns);
cout << " -> (" << x << ", " << y << ")" << endl;
}
} int main(void)
{
Maze maze("src.txt");
maze.showMaze();
cout << endl;
maze.calc();
maze.showPreNode();
cout << endl;
maze.showPath();
cin.get();
}

(图 BFS)走迷宫