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就能连接在一起组成一个区域。
 解题思路:这是一道很基本的深搜例题,当成模板直接来使用吧。
 
上代码:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char map[][];
int vis[][];///标记数组
int dir[][]={{,},{-,},{,},{,-},{,},{-,-},{,-},{-,}};
int n,m;
void DFS(int x,int y)
{
int a,b,i;
vis[x][y]=;
for(i=;i<;i++)
{
a=x+dir[i][];
b=y+dir[i][];
if(a>=&&a<n&&b>=&&b<m&&vis[a][b]==&&map[a][b]=='W')
{
DFS(a,b);
}
}
return ;
}
int main()
{
int count,i,j;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);
getchar();
count=;
for(i=;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(vis[i][j]==&&map[i][j]=='W')
{
count++;
DFS(i,j);
}
}
}
printf("%d\n",count);
return ;
}

Lake Counting(DFS连通图)的更多相关文章

  1. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  2. Lake Counting (DFS)

    N*M的园子,雨后积起了水.八连通的积水背认为是连接在一起的.请求出园子里总共有多少水洼? dfs(Depth-First  Search)  八个方向的简单搜索.... 深度优先搜索从最开始的状态出 ...

  3. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  4. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  5. Poj2386 Lake Counting (DFS)

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

  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. Openjudge1388 Lake Counting【DFS/Flood Fill】

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

  9. POJ2386 Lake Counting 【DFS】

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 D ...

随机推荐

  1. 【解决】could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

    在同一套环境中跑了很多个项目都是用 docker-compose的方式启动的,导致创建的自定义网络过多出现下面的报错 Error response from daemon: could not fin ...

  2. ubuntu系统的软件包管理工具

    ubuntu系统的软件包管理工具有两种,一种是离线管理,另一种是在线管理 1.离线管理 dpkg工具可以对本地存放的deb安装包进行安装,卸载,查看状态等. dpkg -i app_name_vers ...

  3. 使用Scala开发Apache Kafka的TOP 20大好用实践

    本文作者是一位软件工程师,他对20位开发人员和数据科学家使用Apache Kafka的方式进行了最大限度得深入研究,最终将生产实践环节需要注意的问题总结为本文所列的20条建议. Apache Kafk ...

  4. ACM1002:A + B Problem II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  5. C语言顺序表

    顺序表结构可设为一个数组和一个指向尾部的变量,数组用来存放元素,指向尾部的变量在插入元素的时候加一,删除元素的时候减一,始终指向尾部. typedef int elemtype; typedef st ...

  6. leetcode add_binary 采坑记

    尽管add_binary在leetcode中是一个简单难度,但是踩了不少坑,记录一下 描述: 给两个字符串形式的二进制数,要求求和并输出字符串形式的结果,其中a和b均不为空字符串 样例: a=“101 ...

  7. 推荐 的FPGA设计经验(2)-时钟策略优化

    Optimizing Clocking Schemes Avoid using internally generated clocks (other than PLLs) wherever possi ...

  8. 打开所有https网页都提示证书错误

    最近安装了网上下载的ghost系统,可是不管是win7还是xp,打开所有的https网站都提示证书错误.想想现在打击盗版系统的力度不断增加,以前做的比较好的盗版系统网站都已经不再做系统了,现在下载的g ...

  9. 成都Uber优步司机奖励政策(2月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. Redis系列二 Redis数据库介绍

    1.SELECT命令 通过查看配置文件可以知道Redis默认有17个库,从0-16. 默认是在0号库.选择库使用SELECT <dbid>命令.例如选择0号库  SELECT 0 2.DB ...