You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
#include <iostream>
|
|
#include <vector>
|
|
#include <queue>
|
|
using namespace std;
|
|
|
|
int n; // 矩阵行数和列数
|
|
vector<vector<char>> cards; // 卡片状态
|
|
vector<vector<bool>> visited; // 访问标记
|
|
int ans = 0; // 相邻的A面卡片数量最大值
|
|
|
|
// 计算以 (x0, y0) 作为 B 面卡片翻转后相邻的 A 面卡片数量
|
|
int bfs(int x0, int y0) {
|
|
queue<pair<int, int>> q;
|
|
int cnt = 0; // 相邻 A 面卡片数量
|
|
vector<pair<int, int>> dirs{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
|
|
q.push({x0, y0});
|
|
visited[x0][y0] = true;
|
|
if (cards[x0][y0] == 'A')
|
|
cnt++;
|
|
while (!q.empty()) {
|
|
int x = q.front().first;
|
|
int y = q.front().second;
|
|
q.pop();
|
|
// 枚举四个方向
|
|
for (auto &dir : dirs) {
|
|
int nx = x + dir.first;
|
|
int ny = y + dir.second;
|
|
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny] && cards[nx][ny] == 'A') {
|
|
visited[nx][ny] = true;
|
|
cnt++;
|
|
q.push({nx, ny});
|
|
}
|
|
}
|
|
}
|
|
return cnt;
|
|
}
|
|
|
|
int main() {
|
|
cin >> n;
|
|
cards.resize(n, vector<char>(n));
|
|
visited.resize(n, vector<bool>(n, false));
|
|
int ans = 0; // 相邻的A面卡片数量最大值
|
|
// 读入卡片状态
|
|
for (int i = 0; i < n; ++i) {
|
|
for (int j = 0; j < n; ++j) {
|
|
cin >> cards[i][j];
|
|
}
|
|
}
|
|
// 枚举每个 B 面卡片
|
|
for (int i = 0; i < n; ++i) {
|
|
for (int j = 0; j < n; ++j) {
|
|
if (cards[i][j] == 'B') {
|
|
ans = max(ans, bfs(i, j));
|
|
visited = vector<vector<bool>>(n, vector<bool>(n, false));
|
|
}
|
|
}
|
|
}
|
|
// 输出相邻 A 面卡片最多的数量
|
|
cout << ans << endl;
|
|
|
|
return 0;
|
|
} |