黑白路径
2025-01-22 19:00:51
发布于:上海
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
int n, m, cnt1, cnt2;
char a[N][N];
bool st[N][N];
int ne[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
void dfs(int x, int y)
{
if(a[x][y] == '#') cnt1 ++;
else cnt2 ++;
st[x][y] = true;
for(int i = 0; i < 4; i ++) {
int tx = x + ne[i][0];
int ty = y + ne[i][1];
if(tx >= 1 && tx <= n && ty >= 1 && ty <= m
&& !st[tx][ty] && a[tx][ty] != a[x][y]) {
dfs(tx, ty);
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
cin >> a[i][j];
}
}
long long ans = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
if(!st[i][j] && a[i][j] == '#') {
cnt1 = 0, cnt2 = 0;
dfs(i, j);
ans += cnt1 * cnt2;
}
}
}
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个