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/p/2376097.html