You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
898 B

2 years ago
#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;
}