白云校区的水塘

#include<bits/stdc++.h>
using namespace std;
int h, l, ans, nx, ny;
int m[105][105], xy[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

void p(int x,int y)
{
    m[x][y] = 0;
    for(int i = 1; i <=8; i++){
        nx = x + xy[i -1][0];
        ny = y + xy[i -1][1];
        if((nx > 0 && nx <= h) && (ny > 0 && ny <= l) && m[nx][ny]) p(nx, ny);
    }
    return;
}

int main()
{
    while(cin>>h>>l){
        ans = 0;
        for(int i = 1; i <= h; i++)
            for(int j = 1; j <= l; j++) cin>>m[i][j];
        for(int i = 1; i <= h; i++)
            for(int j = 1; j <= l; j++)
                if(m[i][j]) ans++, p(i, j);
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/abs27/p/9284351.html
今日推荐