Submission #74279603
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
const int N = 1010;
// 四个方向:上下左右
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int n, m; // n=行数,m=列数
int a[N][N]; // a[i][j]=1表示黑色(#),0表示白色(.)
bool v[N][N]; // 标记是否已访问
// BFS遍历连通区域,返回1表示该区域被完全包围,0表示接触边缘
int bfs(int x, int y) {
queue<pii> q;
q.push({x, y});
v[x][y] = true;
// flag=1表示初始认为被包围,若接触边缘则置为0
bool flag = true;
while (!q.empty()) {
auto t = q.front();
q.pop();
int cx = t.fi, cy = t.se;
// 检查当前单元格是否在网格边缘
if (cx == 1 || cx == n || cy == 1 || cy == m) {
flag = false;
}
// 遍历四个方向
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
// 合法坐标 + 未访问 + 白色单元格
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !v[nx][ny] && a[nx][ny] == 0) {
v[nx][ny] = true;
q.push({nx, ny});
}
}
}
return flag ? 1 : 0;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
string s;
cin >> s; // 直接读整行,避免字符输入的空格问题
for (int j = 1; j <= m; j++) {
a[i][j] = (s[j-1] == '#'); // 字符串下标从0开始,网格从1开始
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
// 未访问 + 白色单元格
if (!v[i][j] && a[i][j] == 0) {
ans += bfs(i, j);
}
}
}
cout << ans << endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | C - Puddles |
| User | Queryme |
| Language | C++23 (GCC 15.2.0) |
| Score | 300 |
| Code Size | 2036 Byte |
| Status | AC |
| Exec Time | 33 ms |
| Memory | 12664 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 300 / 300 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt, sample_02.txt |
| All | min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, sample_01.txt, sample_02.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| min.txt | AC | 1 ms | 3832 KiB |
| random_01.txt | AC | 14 ms | 12648 KiB |
| random_02.txt | AC | 5 ms | 6584 KiB |
| random_03.txt | AC | 14 ms | 12648 KiB |
| random_04.txt | AC | 8 ms | 10608 KiB |
| random_05.txt | AC | 20 ms | 12580 KiB |
| random_06.txt | AC | 2 ms | 4052 KiB |
| random_07.txt | AC | 21 ms | 12556 KiB |
| random_08.txt | AC | 2 ms | 4080 KiB |
| random_09.txt | AC | 33 ms | 12620 KiB |
| random_10.txt | AC | 24 ms | 12224 KiB |
| random_11.txt | AC | 31 ms | 12648 KiB |
| random_12.txt | AC | 1 ms | 3984 KiB |
| random_13.txt | AC | 27 ms | 12556 KiB |
| random_14.txt | AC | 4 ms | 6772 KiB |
| random_15.txt | AC | 30 ms | 12556 KiB |
| random_16.txt | AC | 9 ms | 7800 KiB |
| random_17.txt | AC | 5 ms | 11604 KiB |
| random_18.txt | AC | 21 ms | 12664 KiB |
| random_19.txt | AC | 21 ms | 12608 KiB |
| random_20.txt | AC | 14 ms | 12628 KiB |
| random_21.txt | AC | 14 ms | 12500 KiB |
| sample_01.txt | AC | 1 ms | 3828 KiB |
| sample_02.txt | AC | 1 ms | 3832 KiB |