problem
一个只包含 # 和 . 的 的网格,求不包含边缘的最大四联通块的数量。
sol
好像是一个水洼加上特判。
看见一个点并且没被搜过就开始搜索,如果没有看见边缘就将答案加一,输出答案即可。
std
#include <bits/stdc++.h>
#define int long long
#define endline putchar('\n')
#define putspace putchar(' ')
using namespace std;
namespace DOG_STD {
inline int read () {
int f = 1, x = 0;
char ch = getchar ();
while (!isdigit (ch)) {
if (ch == 45) f = -1;
ch = getchar ();
}
while (isdigit (ch)) {
x = x * 10 + ch - 48;
ch = getchar ();
}
return f * x;
}
inline void write (int x) {
if (x < 0) {
putchar (45);
x = -x;
}
if (x >= 10) {
write (x / 10);
x = x % 10;
}
putchar (x + 48);
}
const int N = 1e3 + 5;
const int INF = 1 << 30;
const double eps = 1e-6;
int n, vis[N][N], m;
char s[N][N];
bool flag = 1;
void dfs (int x, int y) {
vis[x][y] = 1;
if (x == 1 || x == n || y == 0 || y == m - 1) {
flag = 0; return ;
}
if (s[x-1][y] == '.' && !vis[x-1][y]) dfs (x-1, y);
if (s[x+1][y] == '.' && !vis[x+1][y]) dfs (x+1, y);
if (s[x][y-1] == '.' && !vis[x][y-1]) dfs (x, y-1);
if (s[x][y+1] == '.' && !vis[x][y+1]) dfs (x, y+1);
}
int ans = 0;
void solve () {
n = read ();
m = read ();
for (int i = 1; i <= n; i ++) {
cin >> s[i];
}
for (int i = 2; i < n; i ++) {
for (int j = 1; j < m - 1; j ++) {
if (vis[i][j] || s[i][j] == '#') continue;
flag = 1;
dfs (i, j);
ans += flag;
}
}
write (ans);
return ;
}
}
signed main () {
DOG_STD :: solve ();
return 0;
}