Files
python/TangDou/XiTi/1385.cpp

32 lines
898 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
int ans;
char a[N][N];
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
//其实本质还是一个连通块问题,用bfs和dfs都行,为了图方便,我用了dfs,但是,这个题目我似乎读不明白题,是语文没学好吗?
void dfs(int x, int y) {
a[x][y] = '0';
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
if (a[tx][ty] != '0') 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];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] != '0') ans++, dfs(i, j);
cout << ans << endl;
return 0;
}