汤园是只猫 • 7个月前
using namespace std;
int nuihaoyu3[305][305] = {0}; int res = 0; int nuihaoyu1, nuihaoyu2;
void nuihaoyu(int i, int j) {
if (i < 0 || j < 0 || i >= nuihaoyu1 || j >= nuihaoyu2) {
return;
}
if (nuihaoyu3[i][j] == 0) {
return;
}
nuihaoyu3[i][j] = 0;
nuihaoyu(i + 1, j);
nuihaoyu(i, j + 1);
nuihaoyu(i - 1, j);
nuihaoyu(i, j - 1);
}
int main() {
cin >> nuihaoyu1 >> nuihaoyu2;
for (int i = 0; i < nuihaoyu1; i++) {
for (int j = 0; j < nuihaoyu2; j++) {
cin >> nuihaoyu3[i][j];
}
}
for (int i = 0; i < nuihaoyu1; i++) {
for (int j = 0; j < nuihaoyu2; j++) {
if (nuihaoyu3[i][j] == 1) {
res++;
nuihaoyu(i, j);
}
}
}
cout << res;
return 0;
}
评论:
请先登录,才能进行评论