zoukankan      html  css  js  c++  java
  • 《挑战》2.1 POJ 2386 Lake Counting (简单的dfs)

     1 # include<cstdio>
     2 # include<iostream>
     3 
     4 using namespace std;
     5 
     6 # define MAX 123
     7 
     8 char grid[MAX][MAX];
     9 int nxt[8][2] = { {1,0},{0,-1},{-1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1} };
    10 int n,m;
    11 
    12 int can_move( int x,int y )
    13 {
    14     if ( x>=0&&x<n&&y>=0&&y<m )
    15     {
    16         if ( grid[x][y]=='W' )
    17             return 1;
    18     }
    19     return 0;
    20 }
    21 
    22 
    23 void dfs ( int x,int y )
    24 {
    25     grid[x][y] = '.';
    26     for ( int i = 0;i < 8;i++ )
    27     {
    28         int n_x = x+nxt[i][0], n_y = y+nxt[i][1];
    29         if ( can_move(n_x,n_y) )
    30         {
    31             dfs(n_x,n_y);
    32         }
    33     }
    34     return;
    35 }
    36 
    37 
    38 int main(void)
    39 {
    40     //int n,m;
    41     int res;
    42     while ( scanf("%d%d",&n,&m)!=EOF )
    43     {
    44         res = 0;
    45         for ( int i = 0;i < n;i++ )
    46             scanf("%s",grid[i]);
    47         for ( int i = 0;i < n;i++ )
    48         {
    49             for ( int j = 0;j < m;j++ )
    50             {
    51                 if ( grid[i][j]=='W' )
    52                 {
    53                     dfs(i,j);
    54                     res++;
    55                 }
    56             }
    57         }
    58         printf("%d
    ",res);
    59     }
    60 
    61 
    62     return 0;
    63 }
    A - Lake Counting

    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.

    题目大意:

      就是说一个n*m的网格中,求出联通块的个数。

    解题思路:

      就是一个dfs的基础题,从任意一个满足要求的点开始dfs,求出直到不能走动为止,res++。

    复杂度:

      O(8*m*n)

    代码: 

  • 相关阅读:
    提问的智慧
    Linux下Tomcat的安装配置
    Advanced Puppet 系列的前言
    一个purge参数引发的惨案——从线上hbase数据被删事故说起
    从入门到精通Puppet的实践之路
    Juno Puppet Opertaors Meetup小结
    Openstack配置文件管理的变迁之路
    如何成为一名Top DevOps Engineer
    mod_wsgi的工作模式和配置
    解决PuppetDB Failed to submit 'replace facts'问题
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4675094.html
Copyright © 2011-2022 走看看