The Core Thoughts
核心思想
- DFS (Depth First Search):
- 深度优先搜索
Save the path that can go through using Stack (if it can go through at present, push it to the Stack). When there is no way to go, then go back to the path that has not yet been searched through backtracking. If there is no result when backtracking until the Stack is empty, there is no solution.
使用栈保存可以通过的路径(只要如果目前可以通过,则将该位置压栈)。 当无路可走时,再通过回溯回到尚未搜索到的路径。 如果回溯到栈为空时还没有结果,则无解。
- BFS (Breadth First Search):
- 广度优先搜索
Use the queue to save all the traversable positions and expand the traversable routes in the queue layer by layer until it reaches the exit position. If the last queue is empty, there is no solution.
使用队列保存所有的可遍历位置,将队列中的可遍历路径逐层展开,直到到达出口位置。 如果最后队列为空,则无解。
Source Code
源代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
struct Box {
int x;
int y;
int nextDirection;
Box(int x, int y, int nextDirection) : x(x), y(y), nextDirection(nextDirection) {};
};
struct Box_1 {
int x;
int y;
Box_1* prior; // The pointer to the prior point
Box_1(int x, int y, Box_1* prior) : x(x), y(y), prior(prior) {};
};
vector<vector<int>> GenerateMaze() {
int row, col;
cout << "The number of rows and columns:" << endl;
cin >> row >> col;
cout << "Input the maze:" << endl;
// The maze should be expanded by 2 lengths to comprise the frame of the maze
vector<vector<int>> Maze(row + 2, vector<int>(col + 2, 1));
for (int i = 1; i < row + 1; i++) {
for (int j = 1; j < col + 1; j++) {
cin >> Maze[i][j];
}
}
return Maze;
}
bool IsValidPosition(int x, int

最低0.47元/天 解锁文章
945

被折叠的 条评论
为什么被折叠?



