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. python 学习分享-基础篇

    1.python起手式 写下第一个代码,打印‘hello world’ print('hello world') 2.变量 变量是为了存储信息,在程序中被调用,标识数据名称或类型. 变量定义的规则: ...

  2. Python_Virtualenv及Pycharm配置

    Virtualenv存在的意义 在Python使用过程中,你是否有遇到过同时需要开发多个应用的情况? 假设A应用需要使用DJango1.X版本,而B应用需要使用DJango2.X的版本,而你全局开发环 ...

  3. python学习-- class 类中需要注意的地方

    from django.db import models class Person(models.Model):     name = models.CharField(max_length=30) ...

  4. RHEL 7.3修改网卡命名规则为ethX

    RHEL 7网卡默认命名规则:以太网卡(Ethernet)为enX,无线网卡(WLAN)为wlX,修改网卡命名规则为ethX如下: 1.修改/etc/sysconfig/grub文件,添加net.if ...

  5. 利用MVC模式简单设计jsp分页效果

    利用Mysql创建一个表Car 用Eclipse创建一个Dynamic Web Project 在lib目录下导入Mysql的jar包 创建如下文件 package com.bean; public ...

  6. git基本常用命令总结

    官网:https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E5%8F%96%E5%BE%97%E9%A1%B9%E7%9B%AE%E7%9A% ...

  7. HDU——1019Least Common Multiple(多个数的最小公倍数)

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  8. bzoj1023【SHOI2008】cactus仙人掌图

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1023 求一棵仙人掌的直径 sol :orz YDC神犇 http://ydcydcy1.blo ...

  9. Java面试题之继承、组合、聚合有什么区别

    继承:他是is-a的关系,指一个类继承另外一个类的功能 例如:public class A extends B { } 聚合:他是has-a 例如:public class A{ List<B& ...

  10. Unity 过度光照贴图

    背景:开关窗帘过程,让环境在亮和暗之间过度 事先烘培出亮.暗两张Lighting map.然后代码实现,窗帘开关由动作实现,而代码中通过动作执行进度来过度两张Lighting map void OnA ...