1 题目及要求
1.1 题目描述
如图所示为一个迷宫的分布图,其中灰色表示有障碍物不能通过。现在要从绿色的方格开始,每次只能走一格,制定方案如何能走到红色的终点方格处。给出一个可能的方案即可。
2 解答
2.1 题目分析
用回溯法。每次向一个方向走一格,看能否达到目的地,能则输出方案并返回;否则退回来向其他方向探索。
2.2 代码
#include <iostream>
#include <vector>
using namespace std;
struct Point {
int x, y;
Point(int x1 = 0, int y1 = 0) :x(x1), y(y1) {}
};
bool findPath(vector<vector<bool>> &mat, Point target, vector<Point> &path) {
int rows(mat.size()), c