编程基础 - 队列实例 - 迷宫问题 (Maze Problem)
文章目录
1 迷宫问题简述 (Introduction)
-
迷宫问题:
- 一个长为m,宽为n的长方阵表示迷宫;
- 迷宫表示:
- 0表示迷宫通路
- 1表示迷宫障碍
- 2表示迷宫入口
- 3表示迷宫出口
-
基本要求:
- 非递归程序
- 读取迷宫文件(maze.txt)
- 求出迷宫路径
迷宫文件(maze.txt)为:
12111111111111111111
10100000000010000001
10101010111011111101
10111110110000000001
10000000010111111101
10111110110100000101
10100010111111110101
10101110100000000101
11101010110111111101
10000010110001010111
10101110100111000101
10100000000000010101
10110111111111111101
11110100011110000001
10000101000011111101
10110111011000000101
10010000011111010101
10111111110001010101
10000100000100010001
11111111111131111111
2 C++代码 (C++ Code)
在这里,“读取文件”和“打印结果”不在这里说明。以下只给出求解方法。
2.1 一些定义与声明 (Definition and Declaration)
// Author: https://blog.youkuaiyun.com/DarkRabbit
// Maze Problem
#include <string> // 字符串
#include <vector> // 向量列表
#include <queue> // 队列
#include <fstream> // 文件流,用于打开迷宫文件
#include <unordered_map> // 字典,用于打印迷宫
#include <iomanip> // io库,用于打印限定长度
#include <iostream>
using namespace std;
enum MazePointType
{
Road = '0', // 路标识(可移动,其余不可移动)
Wall = '1', // 墙标识
Start = '2', // 入口标识
Exit = '3', // 出口标识
// 路径标识
Up = 'u',
Right = 'r',
Down = 'd',
Left = 'l'
};
static string MAZE_FILE_NAME = "maze.txt"; // 迷宫文件
// 迷宫坐标
struct MazePoint
{
int row;
int col;
MazePoint(){
}
MazePoint(int r, int c): row(r), col(c){
}
static int GetHash(MazePoint& point)
{
return (point.row << 8) + point.col;
}
static int GetHash(int row, int col)