#include #include #include using namespace std; int n; // 矩阵行数和列数 vector> cards; // 卡片状态 vector> visited; // 访问标记 int ans = 0; // 相邻的A面卡片数量最大值 // 计算以 (x0, y0) 作为 B 面卡片翻转后相邻的 A 面卡片数量 int bfs(int x0, int y0) { queue> q; int cnt = 0; // 相邻 A 面卡片数量 vector> 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(n)); visited.resize(n, vector(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>(n, vector(n, false)); } } } // 输出相邻 A 面卡片最多的数量 cout << ans << endl; return 0; }