Skip to content

Instantly share code, notes, and snippets.

@Shirataki2
Created May 3, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shirataki2/cc21a48a6603d35858ca623ca33d5c66 to your computer and use it in GitHub Desktop.
Save Shirataki2/cc21a48a6603d35858ca623ca33d5c66 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
int w, h;
int C[50][50] = {};
void visit(int x, int y) {
C[x][y] = 0;
for (int ix = x - 1; ix <= x + 1; ix++) {
for (int iy = y - 1; iy <= y + 1; iy++) {
if (ix >= 0 && iy >= 0 && ix < w && iy < h && C[ix][iy] == 1) {
visit(ix, iy);
}
}
}
}
int main() {
while (cin >> w >> h, w) {
int ans = 0;
C[50][50] = {};
REP(j, h) {
REP(i, w) { cin >> C[i][j]; }
}
REP(i, w) {
REP(j, h) {
if (C[i][j] == 1) {
ans++;
visit(i, j);
}
}
}
cout << ans << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment