- zsz_qwq 的博客
-
题解 P21
- @ 2025-12-12 17:57:22
其实这题可以直接使用 BFS。
AC code:
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int main() {
freopen("honey1.in", "r", stdin);
freopen("honey1.out", "w", stdout);
int T;
cin >> T;
while (T--) {
int k;
cin >> k;
vector<string> grid(k);
int start_x = -1, start_y = -1;
for (int i = 0; i < k; ++i) {
cin >> grid[i];
for (int j = 0; j < k; ++j) {
if (grid[i][j] == 'B') {
start_x = i;
start_y = j;
}
}
}
vector<vector<bool> > visited(k, vector<bool>(k, false));
queue<pair<int, int> > q;
q.push({start_x, start_y});
visited[start_x][start_y] = true;
bool found = false;
while (!q.empty() && !found) {
auto [x, y] = q.front();
q.pop();
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= k || ny < 0 || ny >= k) {
continue;
}
if (visited[nx][ny]) {
continue;
}
if (grid[nx][ny] == 'L') {
found = true;
break;
}
if (grid[nx][ny] == 'R' || grid[nx][ny] == 'B') {
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
cout << (found ? "Yes" : "No") << endl;
}
return 0;
}