【POJ2386】Lake Counting

Lake Counting

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22861   Accepted: 11530

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是水洼    求有多少出水洼 相邻的W算是一个连通的水洼

题解:依次搜索每一个点 如果是W ans++并进入dfs 吧相邻的所有点变成.        最后输出ans

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 110;
int N, M;
char fi[maxn][maxn];    //矩形区域

void dfs(int x, int y); //搜索

int main(){
    scanf("%d%d", &N, &M);
    for(int i = 0; i < N; ++i){
        scanf("%s", fi[i]);
    }

    int ans = 0;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < M; ++j){
            if(fi[i][j] == 'W'){    //如果是W进入递归
                dfs(i, j);
                ++ans;              //水洼+1
            }
        }
    }

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

void dfs(int x, int y){
    fi[x][y] = '.';                 //标记为.

    for(int dx = -1; dx <= 1; ++dx){
        for(int dy = -1; dy <= 1; ++dy){
            int nx = x + dx;
            int ny = y +dy;

            if(0 <= nx && nx < N && 0 <= ny && ny < M && fi[nx][ny] == 'W'){    //搜索前后左右
                dfs(nx, ny);                                                    //如果是W进入循环
            }
        }
    }
}
时间: 2024-10-13 18:07:52

【POJ2386】Lake Counting的相关文章

【POJ - 2386】Lake Counting (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

【poj 2386】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

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

【POJ】Parallelogram Counting(HASH,数学之平行四边形)

Parallelogram Counting 题意:输入t表示有t组数据 每组数据输入一个数n,表示有n个点 然后有n行,每行是这个点的(x,y) 问这些点能组成多少个平行四边形 思路:求中点,中点一样的是一个平行四边形. 记录同一个中点的个数sum(初始为1),平行四边形数是(sum-1) * sum / 2; #include<iostream> #include<algorithm> using namespace std; typedef long long ll; con

【计数】【UVA11401】 Triangle Counting

传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. Output 对于每组数据,输出: 对应的方案个数 Sample Input 5 8 0 Sample Output 3 22 Hint n≤1e6. 三个数字x,y,z能组成三角形当且仅当对于任意顺序,都满足x+y>z. Solution 考虑把所有能组成的三角形按照最长边分类.因为三边长度互不相同,所

POJ2386 Lake Counting 【DFS】

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 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 <= 10

Openjudge1388 Lake Counting【DFS/Flood Fill】

http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:   65536kB 描述 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 <= 1

【dfs】POJ2386湖计数

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34735   Accepted: 17246 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 <= 10

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the