首页 > 其他 > 详细

POJ 之2386 Lake Counting

时间:2014-07-27 23:32:29      阅读:564      评论:0      收藏:0      [点我收藏+]
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,布布扣,bubuko.com

POJ 之2386 Lake Counting

原文:http://www.cnblogs.com/yspworld/p/3871980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!