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 <= 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.

Source

睡前水一水。

#include <stdio.h>
#include <string.h> #define maxn 102 char G[maxn][maxn];
int n, m;
const int mov[][2] = {0, 1, 0, -1, 1, 0, -1,
0, 1, -1, -1, 1, 1, 1, -1, -1}; void DFS(int x, int y) {
G[x][y] = '.';
int i, j, nx, ny;
for(i = 0; i < 8; ++i) {
nx = x + mov[i][0];
ny = y + mov[i][1];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && G[nx][ny] == 'W')
DFS(nx, ny);
}
} int main() {
int i, j, ret;
while(scanf("%d%d", &n, &m) == 2) {
for(i = 0; i < n; ++i)
scanf("%s", G[i]);
ret = 0;
for(i = 0; i < n; ++i)
for(j = 0; j < m; ++j)
if(G[i][j] == 'W') {
DFS(i, j);
++ret;
}
printf("%d\n", ret);
}
return 0;
}

POJ2386 Lake Counting 【DFS】的更多相关文章

  1. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  2. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  3. CF990G-GCD Counting【dfs】

    正题 题目链接:https://www.luogu.com.cn/problem/CF990G 题目大意 给出一棵有点权的树,对于每个\(k\)求有多条路径的点权\(gcd\)为\(k\) \(1\l ...

  4. POJ 2386 Lake Counting【BFS】

    题意:给出一个矩形,问有多少块连通的W 当找到W的时候,进行广搜,然后将搜过的W变成点,直到不能再搜,进行下一次广搜,最后搜的次数即为水塘的个数 看的PPT里面讲的是种子填充法. 种子填充算法: 从多 ...

  5. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  6. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  7. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  9. 【dfs】POJ2386湖计数

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34735   Accepted: 17246 D ...

随机推荐

  1. JDK并发基础与部分源码解读

    之前写的一个ppt 搬到博客来

  2. rocketmq源码分析3-consumer消息获取

    使用rocketmq的大体消息发送过程如下: 在前面已经分析过MQ的broker接收生产者客户端发过来的消息的过程,此文主要讲述订阅者获取消息的过程,或者说broker是怎样将消息传递给消费者客户端的 ...

  3. day03_08 变量的重新赋值02

    python自动回收垃圾内存,不用了自动会回收,但是C不会 以下del代码为手动强拆,就是从内存中删除变量名

  4. 九度oj 题目1552:座位问题

    题目描述: 计算机学院的男生和女生共n个人要坐成一排玩游戏,因为计算机的女生都非常害羞,男生又很主动,所以活动的组织者要求在任何时候,一个女生的左边或者右边至少有一个女生,即每个女生均不会只与男生相邻 ...

  5. 性能学习之--loaderunner压测

    打开一个脚本,tools-create Controllwer Scenario,开始场景的设计 一.场景设计--手工测试 1.初始化 2.start vu 一般选择simultaneously,用户 ...

  6. JS实现并集,交集和差集

    var set1 = new Set([1,2,3]);var set2 = new Set([2,3,4]); 并集let union = new Set([...set1, ...set2]); ...

  7. 关于ubuntu的对拍

    感谢夏天dl的blog,写的十分清楚,但是本人对于ubuntu十分不熟悉 所以不怎么会使用. 对拍的可执行文件是sh,就是bash语言 #!bin/bash while true; do ./date ...

  8. 加速和简化构建Docker(基于Google jib)

    赵安家 2019年02月11日阅读 1518 关注 加速和简化构建Docker(基于Google jib) 介绍 其实jib刚发布时就有关注,但是一直没有用于生产,原因有二 基于 spotify/do ...

  9. 长度rem的使用

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...