poj 1562 Oil Deposits (dfs)

http://poj.org/problem?id=1562

水题。

code:

#include<cstdio>
#include<cstring>
int tur[ 8][ 2] = {- 1, - 1, - 10, - 111, - 11011010, - 1} ;
char map[ 101][ 101] ;
int n, m, ans, num ;
bool vis[ 101][ 101] ;
struct node{
     int x, y ;
}coor[ 10005] ;
bool inmap(node p){
     if(p.x>= 0&&p.x<n&&p.y>= 0&&p.y<m)     return  true ;
     return  false ;
}
void bfs( int x,  int y){
    node q[ 10005] ;
    vis[x][y] =  false ;
    ans ++ ;
     int h =  0 ;
     int e =  0 ;
    q[e].x = x ;
    q[e++].y = y ;
     while(h<e){
        node p = q[h++] ;
         for( int i= 0; i< 8; i++){
            node temp ;
            temp.x = p.x + tur[i][ 0] ;
            temp.y = p.y + tur[i][ 1] ;
             if(inmap(temp))
                 if(vis[temp.x][temp.y]){
                    q[e++] = temp ;
                    vis[temp.x][temp.y] =  false ;
                }
        }
    }
}
int main(){
     int i, j ;
     while(~scanf( " %d%d ", &n, &m)&&n+m){
        num =  0 ;
        memset(vis,  falsesizeof(vis)) ;
         for(i= 0; i<n; i++){
            getchar() ;
            scanf( " %s ", map[i]) ;
             for(j= 0; j<m; j++){
                 if(map[i][j]== ' @ '){
                    coor[num].x = i ;
                    coor[num++].y = j ;
                    vis[i][j] =  true ;
                }
            }
        }
        ans =  0 ;
         for(i= 0; i<num; i++){
             if(vis[coor[i].x][coor[i].y])
                bfs(coor[i].x, coor[i].y) ;
        }
        printf( " %d\n ", ans) ;
    }
     return  0 ;} 

转载于:https://www.cnblogs.com/xiaolongchase/archive/2012/03/01/2376097.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值