TYVJ 1127 :: 统计细胞数

于是这个就是典型的fillfloor算法= =,很弱智很弱智的水题。。。

 

#include <stdio.h>

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

int tot, m, n,
     used[50+1][80+1];
char cells[50+1][80+1];

void fillfloor ( long x, long y ){
     long i, xx, yy;
     for ( i = 0; i < 4; i ++ ){
         xx = x+dx[i], yy = y+dy[i];
         if ( xx>=0 && xx<m && yy>=0 && yy<n && !used[xx][yy] &&
              cells[xx][yy]-'0' > 0 ){
              used[x+dx[i]][y+dy[i]] = 1;
              fillfloor ( x+dx[i], y+dy[i] );
         }
     }
}

int main(){
    scanf ( "%d %d", &m, &n );
    long i, j;
    for ( i = 0; i < m; i ++ ){
        getchar();
        for ( j = 0; j < n; j ++ )
            scanf ( "%c", &cells[i][j] );
    }
    
    for ( i = 0; i < m; i ++ )
        for ( j = 0; j < n; j ++ )
            if ( ( cells[i][j]-'0' > 0 ) && !used[i][j]  ){
               used[i][j] = 1;
               tot ++;
               fillfloor ( i, j );
            }
            
    printf ( "%d\n", tot );
    getchar(), getchar();
    return 0;
}

posted on 2010-11-10 21:59  Jasper Ho  阅读(365)  评论(0编辑  收藏  举报

导航