题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
#include <iostream>
#include <queue>
#include <sys/ucontext.h>
#include <vector>
using namespace std;
#define N 100
int dp[N][N];
int n;
int m;
int dfs(int x, int y, vector<vector<int>>& q) {
if (x >= n || x < 0 || y < 0 || y >= m) return -1;
if(x == n - 1 && y == m - 1) return 1;
if(dp[x][y] != 0) return -1;
q.push_back({x, y});
dp[x][y] = -1;
int dirs[4][2] = {
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};
for(int i = 0; i < 4; i++){
if(dfs(x + dirs[i][0], y + dirs[i][1], q) == 1) return 1;
}
q.pop_back();
return -1;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> dp[i][j];
}
}
vector<vector<int>> q;
// q.push_back({0, 0});
dfs(0, 0, q);
q.push_back({n - 1, m - 1});
for(vector<int> t: q) {
cout << "(" << t[0] << "," << t[1] << ")" << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
查看20道真题和解析