Skip to content

Commit

Permalink
Add 06080.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiyabye committed Jul 14, 2024
1 parent 0807630 commit 9d9ef9a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 06xxx/06080.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
using namespace std;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {1, 0, -1, 1, -1, 1, 0, -1};

void dfs(int r, int c, vector<vector<int>> &v, int y, int x) {
v[y][x] = 0;
for (int i=0; i<8; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || ny >= r || nx < 0 || nx >= c) continue;
if (v[ny][nx]) dfs(r, c, v, ny, nx);
}
}

void solve(void) {
int r, c; cin >> r >> c;
vector<vector<int>> v(r, vector<int>(c));
for (int i=0; i<r; i++) for (int j=0; j<c; j++) cin >> v[i][j];

int ans = 0;
for (int i=0; i<r; i++) for (int j=0; j<c; j++) {
if (!v[i][j]) continue;
dfs(r, c, v, i, j);
ans++;
}
cout << ans;
}

int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);

solve();
return 0;
}

0 comments on commit 9d9ef9a

Please sign in to comment.