三个测试点错 求解
2023-08-24 22:59:20
发布于:四川
14阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node {
int row;
int col;
int time;
};
int main() {
int R, C;
cin >> R >> C;
vector<vector<char>> maze(R, vector<char>(C));
vector<vector<bool>> visited(R, vector<bool>(C, false));
queue<Node> q;
Node den;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cin >> maze[i][j];
if (maze[i][j] == 'S') {
q.push({i, j, 0});
visited[i][j] = true;
} else if (maze[i][j] == 'D') {
den.row = i;
den.col = j;
}
}
}
while (!q.empty()) {
Node current = q.front();
q.pop();
if (current.row == den.row && current.col == den.col) {
cout << current.time << endl;
return 0;
}
int dr[] = {1, -1, 0, 0};
int dc[] = {0, 0, 1, -1};
for (int i = 0; i < 4; i++) {
int newRow = current.row + dr[i];
int newCol = current.col + dc[i];
if (newRow >= 0 && newRow < R && newCol >= 0 && newCol < C && !visited[newRow][newCol] && maze[newRow][newCol] != 'X' && maze[newRow][newCol] != '*') {
q.push({newRow, newCol, current.time + 1});
visited[newRow][newCol] = true;
}
}
}
cout << "KAKTUS" << endl;
return 0;
}
全部评论 1
what are you bfs
2024-05-09 来自 广东
0
有帮助,赞一个