一、题解
#include <iostream>
#include <vector>
using namespace std;
class Solution {
private:
vector<vector<string>> result;
public:
vector<vector<string>> solveMaze(vector<string> &maze, int x, int y) {
vector<string> each_case = maze;
backtracking(maze, 1, 1);
return result;
}
void backtracking(vector<string> &each_case, int row, int col) {
if (each_case.at(row).at(col) == '*') {
result.emplace_back(each_case);
return;
}
if (each_case.at(row).at(col) == '0') {
each_case.at(row).at(col) = '1';
backtracking(each_case, row - 1, col);
backtracking(each_case, row + 1, col);
backtracking(each_case, row, col - 1);
backtracking(each_case, row, col + 1);
each_case.at(row).at(col) = '0';
}
}
};
int main(int argc, char *argv[]) {
Solution solution;
vector<string> maze = {{'#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '0', '0', '#', '#', '#', '#', '#'},
{'#', '#', '0', '#', '0', '0', '#', '#'},
{'#', '0', '0', '#', '0', '#', '*', '#'},
{'#', '#', '0', '0', '0', '#', '0', '#'},
{'#', '#', '0', '#', '0', '0', '0', '#'},
{'#', '#', '0', '0', '0', '#', '0', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#'}};
for (const auto &i: solution.solveMaze(maze, 1, 1)) {
for (const auto &j: i) {
cout << j << endl;
}
cout << endl;
}
}
二、运行结果
########
#11#####
##1#00##
#01#0#*#
##100#1#
##1#111#
##111#0#
########
########
#11#####
##1#00##
#01#0#*#
##111#1#
##0#111#
##000#0#
########
![在这里插入图片描述](https://img-blog.csdnimg.cn/a4f96db7a1d44356b36cef89c0ba7800.png#pic_center)