Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20003   Accepted: 10063

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.

 题目分析:
     给出一块字符串区域代表地图,W代表water(水),其余代表土地,水的连接代表 湖,要我们计算这个地图里有几个湖。
 
算法分析:注意用读入字符串的。。。未完待续!
 
 
 
 
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char map[101][101];
int vt[101][101];
int dir[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int n, m;

void dfs(int x, int y)
{
    int i;
    for(i=0; i<8; i++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(map[xx][yy]=='W' && xx>=0 && xx<m && yy>=0 && yy<n && vt[xx][yy]==0 )
        {
            vt[xx][yy] = 1;
            dfs(xx, yy);
        }
    }
}

int main()
{
    int cnt;
    int i, j;
    while(scanf("%d %d%*c", &m, &n)!=EOF)
    {
        if(m==0 && n==0)
            break;
        cnt = 0;

memset(vt, 0, sizeof(vt));

for(i=0; i<m; i++)
        {
            scanf("%s", map[i]);
        }
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(map[i][j]=='W' && vt[i][j]==0)
                {
                    vt[i][j] = 1;
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}

POJ 之2386 Lake Counting的更多相关文章

  1. POJ No.2386 Lake Counting

    题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...

  2. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  3. POJ 2386 Lake Counting

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

  4. [POJ 2386] Lake Counting(DFS)

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

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

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

  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(简单DFS深搜)

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

  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. c++打印蛇形矩阵

    一个m*n的矩阵里按照下图形式填充,最后形成的矩阵即为蛇形矩阵,下图是m=4, n =5时的蛇形矩阵: 方法一:逐层循环 #include <iostream> using namespa ...

  2. webuploader插件使用中的一点东西

    本人绝对菜鸟,高手勿喷 菜鸟开发中的解决方法,高手勿喷 1.针对同一应用中不同的类别,存放不同的路径 在页面中添加,hidden属性的标记,如:    type="hidden" ...

  3. Google 商店

    Google 商店地址:https://store.google.com/

  4. SAP abab 设定动态标题 SET TITLEBAR XXXX WITH XXXX

    供ALV输出EXCEL时调用. 动态地改变TITLEBAR上的文字描写叙述,详细使用方法例如以下: 1.双击TITLEBAR.进入TITLEBAR的编辑框,在这里写入&1,相当于宏定义的宏的參 ...

  5. Linux - vim安装 配置与使用

    一 Vim 简单介绍 曾经一直用vi,近期開始使用 vim,以下将两者做一下比較. vi和vim都是word=%E5%A4%9A%E6%A8%A1&fr=qb_search_exp&i ...

  6. [译]GLUT教程 - 交换菜单

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Swapping Menus GLUT甚至可以在应用程序过 ...

  7. DataUml Design 教程1-初识

        DataUml Design 是面向开发人员使用的一个永久免费的软件,提高软件的开发效率和代码的规范度.它主要包括三大功能,数据模型.代码生成和UML建模,数据模型功能类似于PowerDesi ...

  8. 《HBase in Action》 第三章节的学习总结 ---- 如何编写和运行基于HBase的MapReduce程序

    HBase之所以与Hadoop是最好的伙伴,我理解就因为两点:1.HADOOP的HDFS,为HBase提供了分布式的存储方式:2.HADOOP的MR为HBase提供的分布式的计算方法.u 其中第一点, ...

  9. protobuf编译安装

    为什么选择protobuf,而不选择thift和avro,原因大概几点吧,网上对比的文章很多,我主要关注以下几点 1.protobuf序列化性能最好,序列化后字节数最少. 2.protobuf是单纯的 ...

  10. shiro设置session超时

    通过api:Shiro的Session接口有一个setTimeout()方法 //登录后,可以用如下方式取得session SecurityUtils.getSubject().getSession( ...