'commit'
This commit is contained in:
48
TangDou/Bfs/1381_bfs.cpp
Normal file
48
TangDou/Bfs/1381_bfs.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
#define x first
|
||||
#define y second
|
||||
typedef pair<int, int> PII;
|
||||
const int N = 110;
|
||||
int g[N][N];
|
||||
int n, m;
|
||||
|
||||
int dx[] = {-1, 0, 1, 0}; //上右下左
|
||||
int dy[] = {0, 1, 0, -1}; //上右下左
|
||||
|
||||
int ans;
|
||||
queue<PII> q;
|
||||
|
||||
void bfs() {
|
||||
while (q.size()) {
|
||||
auto u = q.front();
|
||||
q.pop();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int tx = u.x + dx[i], ty = u.y + dy[i];
|
||||
if (g[tx][ty]) {
|
||||
g[tx][ty] = 0;
|
||||
q.push({tx, ty});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= m; j++)
|
||||
cin >> g[i][j];
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
if (g[i][j]) {
|
||||
ans++;
|
||||
g[i][j] = 0;
|
||||
q.push({i, j});
|
||||
bfs();
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user