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. Linux下利用phpize安装memcashe的php源码扩展包

    phpize是php的一种构建工具,为PHP扩展准备构建环境,通过phpize可以编译php的扩展源码文件为php扩展模块. 一.安装 phpize工具可以通过安装php-dev包自动集成安装.安装完 ...

  2. MySQL5.5中文支持

    1. /etc/my.cnf.d/client.cnf [client] #password = [your_password] port = 3306 socket = /tmp/mysql.soc ...

  3. 计算机图形学(二)输出图元_6_OpenGL曲线函数_2_中点画圆算法

    中点画圆算法        如同光栅画线算法,我们在每一个步中以单位间隔取样并确定离指定圆近期的像素位置.对于给定半径r和屏幕中心(xc,yc),能够先使用算法计算圆心在坐标原点(0, 0)的圆的像素 ...

  4. 【整理】Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来或打印出来却是乱码

    转自:http://www.crifan.com/python_already_got_correct_encoding_string_but_seems_print_messy_code/ [背景] ...

  5. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  6. angualejs

    http://segmentfault.com/a/1190000000347412 http://www.xker.com/page/e2015/06/199141.html http://www. ...

  7. 同样的代码在java和c++中结果不同

    #include <iostream> using namespace std; /* run this program using the console pauser or add y ...

  8. Android hellocharts 柱形图详解

    近日需要做图表结构的项目,目前最火的就是hellocharts  和MPAndroidChart  相对来说hellocharts集成比较简单: 官网地址   https://github.com/l ...

  9. Pollard-Rho大整数拆分模板

    随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...

  10. POJ1942

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24236   Accepted: 6006 ...