Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
136643 蔡悠然 迷宫问题 C++ 编译错误 0 0 MS 0 KB 1129 2024-03-09 11:18:04

Tests(0/0):


#include <vector> #include <iostream> using namespace std; bool vis[105][105]; vector<int> ans[2]; int m, n, map[105][105], move[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; void push_ans(int x, int y) { ans[0].push_back(x); ans[1].push_back(y); } bool in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; } bool dfs(int x, int y) { if (x == m && y == n) { push_ans(m, n); return true; } vis[x][y] = true; for (int i = 0; i < 4; i++) { int tx = x + move[i][0]; int ty = y + move[i][1]; if (in(tx, ty) && !vis[tx][ty] && !map[tx][ty]) { if (dfs(tx, ty)) { push_ans(x, y); return true; } } } return false; } int main() { cin >> m >> n; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> map[i][j]; } } if (!dfs(1, 1)) { cout << -1 << endl; } else { for (int i = ans[0].size() - 1; i >= 0; i--) { cout << ans[0][i] << ' ' << ans[1][i] << endl; } } return 0; }


测评信息: