第一行输入整数
。
随后
行,每行
个字符构成吴铭市的地图,只含 `'.'` 与 `'#'`。已知边界四条边全部为已经被洪水淹没的区域。
输出一个整数,表示一天后会完全消失的空地区域数量。
1 .
0
from collections import deque n = int(input()) a = [list(input()) for _ in range(n)] # Create a map to store which island ID each cell belongs to island_map = [[0] * n for _ in range(n)] dirs = [(0, -1), (0, 1), (-1, 0), (1, 0)] def bfs_label(si, sj, label): q = deque([(si, sj)]) island_map[si][sj] = label while q: i, j = q.popleft() for y, x in dirs: r, c = i + y, j + x if 0 <= r < n and 0 <= c < n and a[r][c] == '#' and island_map[r][c] == 0: island_map[r][c] = label q.append((r, c)) # 1. Label each original island with a unique ID (1, 2, 3...) island_count = 0 for i in range(n): for j in range(n): if a[i][j] == '#' and island_map[i][j] == 0: island_count += 1 bfs_label(i, j, island_count) # 2. Identify which cells will survive the flood # (A cell survives if it's NOT adjacent to any '.') surviving_ids = set() for i in range(n): for j in range(n): if a[i][j] == '#': is_submerged = False for y, x in dirs: r, c = i + y, j + x if r < 0&nbs***bsp;r >= n&nbs***bsp;c < 0&nbs***bsp;c >= n&nbs***bsp;a[r][c] == '.': is_submerged = True break # If this specific cell is NOT submerged, its island survives if not is_submerged: surviving_ids.add(island_map[i][j]) # 3. Total islands - Islands that had at least one surviving cell print(island_count - len(surviving_ids))
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
int N;
cin >> N;
vector<string> grid(N);
for (int i = 0; i < N; i++) {
cin >> grid[i];
}
// 标记一天后会淹没的 '#'
vector<vector<bool>> will_flood(N, vector<bool>(N, false));
// 第一次遍历:找出所有与 '.' 相邻的 '#',标记为 will_flood
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == '#') {
for (int d = 0; d < 4; d++) {
int ni = i + dx[d];
int nj = j + dy[d];
if (ni >= 0 && ni < N && nj >= 0 && nj < N && grid[ni][nj] == '.') {
will_flood[i][j] = true;
break;
}
}
}
}
}
// 找出所有 '#' 的连通区域
vector<vector<bool>> visited(N, vector<bool>(N, false));
vector<vector<pair<int, int>>> regions;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == '#' && !visited[i][j]) {
// BFS 找连通区域
queue<pair<int, int>> q;
vector<pair<int, int>> region;
q.push({i, j});
visited[i][j] = true;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
region.push_back({x, y});
for (int d = 0; d < 4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 0 && nx < N && ny >= 0 && ny < N &&
grid[nx][ny] == '#' && !visited[nx][ny]) {
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
regions.push_back(region);
}
}
}
// 统计完全消失的区域
int vanished_count = 0;
for (auto& region : regions) {
bool all_flooded = true;
for (auto [x, y] : region) {
if (!will_flood[x][y]) {
all_flooded = false;
break;
}
}
if (all_flooded) {
vanished_count++;
}
}
cout << vanished_count << endl;
return 0;
} #include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;
using pii = pair<int, int>;
int n;
vector<vector<char>> grid;
set<pii> visited;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
vector<pii> BFS(pii start) {
queue<pii> q;
vector<pii> component; // 存储连通区域的所有坐标
q.push(start);
visited.insert(start);
component.push_back(start);
while (!q.empty()) {
pii cur = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int nr = cur.first + dr[k];
int nc = cur.second + dc[k];
// 检查边界
if (nr < 0 || nc < 0 || nr >= n || nc >= n) continue;
// 必须是'#' 并且未访问过
if (grid[nr][nc] == '#' && !visited.count({nr, nc})) {
visited.insert({nr, nc});
q.push({nr, nc});
component.push_back({nr, nc}); // 记录区域坐标
}
}
}
return component;
}
bool judge(const vector<pii>& component) {
for (const auto& p : component) {
int r = p.first;
int c = p.second;
bool is_survivor = true;
// 检查四个方向
for (int k = 0; k < 4; k++) {
int nr = r + dr[k];
int nc = c + dc[k];
// 存活格子的四个邻居必须都是 '#'
// 边界已知是 '.',所以越界或遇到 '.' 都会导致该格子被淹没
if (nr < 0 || nc < 0 || nr >= n || nc >= n || grid[nr][nc] == '.') {
is_survivor = false;
break;
}
}
if (is_survivor) {
// 找到一个四面都是'#'的格子,则该区域存活
return true;
}
}
return false;
}
int main() {
cin >> n;
if (n == 1) {
char c;
cin >> c;
}
grid.resize(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// 找到一个新的未访问过的空地区域
if (grid[i][j] == '#' && !visited.count({i, j})) {
// 找到区域并判断是否存活
vector<pii> component = BFS({i, j});
// 如果区域不存活 (!judge),则计数器 +1
if (!judge(component)) {
cnt++;
}
}
}
}
cout << cnt << endl;
return 0;
}