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.

63 lines
1.7 KiB

2 years ago
/*
N*M0
1N*M
N*M
N=4M=54*5
4*53绿
4 5
1 1 0 0 0
1 0 1 0 0
1 0 0 0 0
1 1 0 1 1
3
*/
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 110;
int a[N][N];
int n, m;
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
void bfs(int x, int y) {
a[x][y] = 0;
queue<PII> q;
q.push({x, y});
while (q.size()) {
auto u = q.front();
q.pop();
for (int i = 0; i <= 3; i++) {
int tx = u.x + dx[i], ty = u.y + dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
if (a[tx][ty]) {
a[tx][ty] = 0;
q.push({tx, ty});
}
}
}
}
int res;
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]) {
bfs(i, j);
res++;
}
}
printf("%d", res);
return 0;
}