- 【例2.1】Hello World
开挂走迷宫
- 2025-1-26 19:21:48 @
说明
邋稽正在玩走迷宫游戏,但它发现傻*的迷宫设计者设计的迷宫是无解的!于是它当即选择了开挂,怎么有动物这么傻, 获得了一次炸毁墙壁的机会,请问:走迷宫的最短路径是什么?如果炸毁了墙壁也无法通关,则输出"No"。
输入格式
第一行两个数,N和M,表示迷宫的长和宽。 剩下N行,输出迷宫,包含"Q"起点,"Z"终点,"#"墙壁,"."可走的路,格式见数据。
输出格式
N行,为迷宫的最短路径,走过的路为"o",炸毁的墙壁为"!",格式见数据。
输入数据1
5 5
Q#...
.#.#.
.#.##
.#.#.
...#Z
输出数据1
Q#...
o#.#.
o#.##
o#.#.
ooo!Z
输入数据2
3 3
Q##
.##
##Z
输出数据2
No
数据范围
3 条评论
-
2024qyyz026 LV 5 @ 2025-3-5 21:43:17
本人为了试验DeepSeek,问了它这个问题
以下是它写的代码
#include <iostream> #include <vector> #include <queue> #include <tuple> #include <algorithm> using namespace std; // 定义方向数组:上、下、左、右 const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; // 定义结构体表示状态 struct State { int x, y; // 当前位置 bool used; // 是否使用过炸墙机会 int steps; // 当前步数 State* prev; // 前驱状态 }; // 解决迷宫问题 vector<string> solve_maze(int N, int M, vector<string>& maze) { // 找到起点和终点的位置 pair<int, int> start, end; for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { if (maze[i][j] == 'Q') { start = {i, j}; } else if (maze[i][j] == 'Z') { end = {i, j}; } } } // BFS队列 queue<State*> q; State* initial = new State{start.first, start.second, false, 0, nullptr}; q.push(initial); // 记录访问状态 vector<vector<vector<bool>>> visited(N, vector<vector<bool>>(M, vector<bool>(2, false))); visited[start.first][start.second][false] = true; // BFS State* final_state = nullptr; while (!q.empty()) { State* current = q.front(); q.pop(); // 如果到达终点 if (current->x == end.first && current->y == end.second) { final_state = current; break; } // 遍历四个方向 for (int i = 0; i < 4; ++i) { int nx = current->x + dx[i]; int ny = current->y + dy[i]; if (nx >= 0 && nx < N && ny >= 0 && ny < M) { // 如果下一个点是可走的路或终点 if (maze[nx][ny] == '.' || maze[nx][ny] == 'Z') { if (!visited[nx][ny][current->used]) { visited[nx][ny][current->used] = true; State* next = new State{nx, ny, current->used, current->steps + 1, current}; q.push(next); } } // 如果下一个点是墙壁且没有使用过炸墙机会 else if (maze[nx][ny] == '#' && !current->used) { if (!visited[nx][ny][true]) { visited[nx][ny][true] = true; State* next = new State{nx, ny, true, current->steps + 1, current}; q.push(next); } } } } } // 如果没有找到路径 if (!final_state) { return {"No"}; } // 回溯路径 vector<pair<int, int>> path; State* temp = final_state; while (temp) { path.push_back({temp->x, temp->y}); temp = temp->prev; } reverse(path.begin(), path.end()); // 标记路径 for (size_t i = 0; i < path.size(); ++i) { int x = path[i].first; int y = path[i].second; if (maze[x][y] == '#') { maze[x][y] = '!'; } else if (maze[x][y] == '.') { maze[x][y] = 'o'; } } return maze; } int main() { // 输入处理 int N, M; cin >> N >> M; vector<string> maze(N); for (int i = 0; i < N; ++i) { cin >> maze[i]; } // 解决问题 vector<string> result = solve_maze(N, M, maze); // 输出结果 if (result.size() == 1 && result[0] == "No") { cout << "No" << endl; } else { for (const string& row : result) { cout << row << endl; } } return 0; }
-
2025-1-26 20:23:07@
#include<iostream> using namespace std; int num[15][15]; int main(){ int n,m; cin>>n>>m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>num[i][j]; } } cout<<"No" return 0;
至少会对亿点点 -
2025-1-26 20:17:46@
自己出的题,可以
不写题解……不像某穿山乙……
👎 1
- 1
信息
- ID
- 5798
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 141
- 已通过
- 68
- 上传者