Lake Counting

Time Limit: 1000MS     Memory Limit: 65536K

Total Submissions: 17917     Accepted: 9069

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

思路

从任意的W开始,不停地把邻接的部分用'.'代替。 1次DFS后与初始的这个W连接的所有W就都被替
换成了'.',因此直到图中不再存在W为止,总共进行DFS的次数就是答案了。 8个方向共对应了8种
状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×N×M)=O(N×M)。

#include<stdio.h>
#define MAX_N 105
#define MAX_M 105
char field[MAX_N][MAX_M];
int N,M;

void dfs(int x,int y)
{

    int dx,dy,nx,ny;

    field[x][y] = '.'; //将现在位置替换为'.'; 

    for (dx = -1;dx < 2;dx++)  //遍历移动的8个方向
    {
        for (dy = -1;dy < 2;dy++)
        {
            nx = x + dx,ny = y + dy;
            if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W')
            {                                   //判断(nx,ny)是不是在园子内
                dfs(nx,ny);
            }
        }
    }
}

int main()
{
    int i,j,res = 0;
    char tmp;
    scanf("%d%d",&N,&M);
    getchar();
    for (i=0;i<N;i++)
    {
        for (j=0;j<M;j++)
        {
            scanf("%c",&field[i][j]);
            if (j==M-1)
            {
                scanf("%c",&tmp);
            }
        }
    }

    for (i = 0;i < N;i++)
    {
        for (j = 0;j < M;j++)
        {
            if (field[i][j] == 'W')
            {
                dfs(i,j);
                res++;
            }
        }
    }

    printf("%d\n",res);
    return 0;
}

POJ 2386 Lake Counting(深搜)的更多相关文章

  1. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  2. POJ 2386 Lake Counting (简单深搜)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  3. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  4. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  5. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  6. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  7. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  8. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  9. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

随机推荐

  1. WPF路径动画(动态逆向动画)

    WPF 中的Path.Data 不再多介绍,M开始坐标点 C弧度坐标点 L 直线坐标点 <Path x:Name="path0" Data="M 10,100 C ...

  2. OFFSET IN 使用举例

    本文将结合具体实例阐述OFFSET IN的使用方法.注意:这是我第一次写OFFSET IN约束,本文仅供参考.阅读本文前需要了解时序收敛的基本概念,OFFSET IN和Period的相关知识,可先阅读 ...

  3. ContentProvider数据访问详解

    ContentProvider数据访问详解 Android官方指出的数据存储方式总共有五种:Shared Preferences.网络存储.文件存储.外储存储.SQLite,这些存储方式一般都只是在一 ...

  4. HDU5892~HDU5901 2016网络赛沈阳

    A.题意: 有一个n×n的格子, 有50种怪物. 有m个操作, 每次操作会往一个矩形区域放怪物, 每个格子放相同数目的怪物, 或者查询当前50种怪物的奇偶性. 分析:用2^50表示怪物的奇偶,然后就是 ...

  5. NumPy 上手一个例子 vectorsum.py

    NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩 ...

  6. 【Zeyphr】保存json到数据库

    方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...

  7. ViewHolder数据错乱BUG

    需求是这样的,在列表中用一个图标标示某个item是已经被接下或者完成的任务. 对于文件有这样的操作,进入列表后第一页面展示正常,但是加载更多后同样位置出现了同样的标志.这不是我想要的效果 我的解决办法 ...

  8. js_RGB转16进制(rgb2hex)

    输入:rgb(13,0,255) 输出:#0d00ff 在线颜色转换工具:http://www.atool.org/colorpicker.php 1 2 3 4 5 6 7 8 9 function ...

  9. 二、处理MVC多级目录问题——以ABP为基础架构的一个中等规模的OA开发日志

    就个人感觉而言.ASP.NET MVC是一种非常反人类的设计.(我没有接触过Java的MVC,不知道两者是否一样.如果一样,那么搞Java的同学也挺可怜.)尤其是MVC的路由机制,灰常灰常反动.路由所 ...

  10. spoj705 后缀数组求不同子串的个数

    http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a stri ...