zoukankan      html  css  js  c++  java
  • [POJ] 2386 Lake Counting

    Lake Counting
    Time Limit: 1000MS      Memory Limit: 65536K
    Total Submissions: 38083        Accepted: 18919
    Description
    
    Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. 
    
    Given a diagram of Farmer John's field, determine how many ponds he has.
    Input
    
    * Line 1: Two space-separated integers: N and M 
    
    * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
    Output
    
    * Line 1: The number of ponds in Farmer John's field.
    Sample Input
    
    10 12
    W........WW.
    .WWW.....WWW
    ....WW...WW.
    .........WW.
    .........W..
    ..W......W..
    .W.W.....WW.
    W.W.W.....W.
    .W.W......W.
    ..W.......W.
    Sample Output
    
    3
    Hint
    
    OUTPUT DETAILS: 
    
    There are three ponds: one in the upper left, one in the lower left,and one along the right side.
    Source
    
    USACO 2004 November

    联通块,强迫自己用一用bfs。

    //Writer:GhostCai && His Yellow Duck
    
    #include<iostream>
    #include<queue>
    #include<cstdio>
    #define MAXN 200
    using namespace std;
    
    int m,n;
    char a[MAXN][MAXN];
    int ans;
    bool vis[MAXN][MAXN];
    
    int dx[8]={0,1,1,1,0,-1,-1,-1};
    int dy[8]={1,1,0,-1,-1,-1,0,1};
    
    struct point{
        int x,y; 
    }node,r;
    
    void bfs(int x,int y){
        queue<point> Q;
        node.x = x;
        node.y = y;
        Q.push(node);
        while(!Q.empty() ){
            r=Q.front() ;
            Q.pop() ;
            for(int i=0;i<=7;i++){
                int nx=r.x+dx[i],ny=r.y+dy[i];
                if(nx<1||nx>m||ny<1||ny>n) continue;
                if(!vis[nx][ny]&&a[nx][ny]=='W'){
                    vis[nx][ny]=1;
                    a[nx][ny]='.';
                    node.x = nx;
                    node.y = ny;
                    Q.push(node); 
                }
            }
        } 
    }
    
    
    
    int main(){
        cin>>m>>n;
        int i,j;
        for(i=1;i<=m;i++){
            scanf("%s",a[i]+1); 
        }
        for(i=1;i<=m;i++){
            for(j=1;j<=n;j++){
                if(!vis[i][j]&&a[i][j]=='W'){
                    a[i][j]='.';
                    bfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
        return 0; 
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247540.html

  • 相关阅读:
    Java并发理论简介
    【SQL】- 基础知识梳理(八)
    【SQL】- 基础知识梳理(七)
    【SQL】- 基础知识梳理(六)
    【SQL】- 基础知识梳理(五)
    【SQL】- 基础知识梳理(四)
    【SQL】- 基础知识梳理(三)
    '{ }'在不同上下文中的作用
    模块化你的JS代码
    利用模板将HTML从JavaScript中抽离
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247540.html
Copyright © 2011-2022 走看看