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

题解

这道题显然是一道入门搜索题

【涨涨自信2333】

只要八个方向dfs就好

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n,m,ans;
int xx[]={,,,,,,-,-,-},
yy[]={,,-,,,-,,,-};
char a[][];
bool map[][],vis[][]; void dfs(int a,int b)
{
for(int i=;i<=;++i)
{
int x=xx[i]+a,y=yy[i]+b;
if(map[x][y]&&!vis[x][y])
{
vis[x][y]=;
dfs(x,y);
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
{
scanf("%s",a[i]);
for(int j=;j<m;++j)
if(a[i][j]=='W')map[i][j+]=;
}
for(int i=;i<=n;++i)
for(int j=;j<=m;++j)
if(map[i][j]&&!vis[i][j])
{
vis[i][j]=;
dfs(i,j);
ans++;
}
printf("%d",ans);
}

【dfs】POJ2386湖计数的更多相关文章

  1. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  2. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  3. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  4. 洛谷P1596 [USACO10OCT]湖计数Lake Counting

    https://www.luogu.org/problemnew/show/P1596 连通块水题... 大体思路是找到是水坑的坐标然后就开始不断递归,往八个方向搜,把连在一起的都标记一遍直到找不到为 ...

  5. [USACO10OCT]湖计数Lake Counting 联通块

    题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...

  6. $P1596 [USACO10OCT]湖计数Lake Counting$

    \(problem\) 其实这题吧\(DFS\)好写一点(大雾 所以就不讲\(DFS\)了 em \(BFS\)的话 主要是 判重. 方向. 队列.(没了吧 至于位置 用两个队列?还是\(pair\) ...

  7. P1596 【[USACO10OCT]湖计数Lake Counting】

    可爱的题面君~~ 个人感觉这题还是很简单的,就是一个完全不加工的找联通块个数 个人解题思路是先读入,然后循环一遍,遇到水就dfs,并把这个w所在的联通块“删除”,并在答案上加一 最后输出答案 具体注释 ...

  8. Luogu P1596 [USACO10OCT]湖计数Lake Counting

    题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...

  9. poj2386

    湖计数描述由于最近的降雨,水汇集在不同的地方,在农民约翰的领域,这是代表一个长方形的N×M(1μ= 100:1 = M = 100)平方.每一方都包含水(’w')或干燥的土地(“.”).农民约翰想弄清 ...

随机推荐

  1. (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数&lt;=3,输出剩下的人 )

    题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  3. C++ - 复制容器(container)的元素至还有一个容器

    复制容器(container)的元素至还有一个容器 本文地址: http://blog.csdn.net/caroline_wendy C++复制容器(container)元素, 能够使用标准库(ST ...

  4. hdu5304 Eastest Magical Day Seep Group&#39;s Summer 状压dp+生成树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5304 16个点的无向图,问能生成多少个n条边的连通图.(即多一条边的树) 先n^3 * 2^n 枚举全部的 ...

  5. 把握linux内核设计思想(十三):内存管理之进程地址空间

    [版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet.文章仅供学习交流,请勿用于商业用途] 进程地址空间由进程可寻址的虚拟内存组成,Linux 的虚拟地址空间为0~4G字 ...

  6. OS X第三方类库管理工具cocoapods入门

    一.简介 什么是cocoapods?--是OS X和iOS下一个第三方类库管理工具.通过cocoapods,可以为项目添加"Pods"依赖库,并且管理其版本. cocoapods好 ...

  7. TableML-GUI篇(Excel编译/解析工具)

    项目情况 本文接上篇TableML Excel编译/解析工具,本文主要介绍GUI工具的使用,及配置项,如果你想了解此工具更加详细的说明,请阅读上篇文章. 项目地址:https://github.com ...

  8. Mybatis中模糊查询的各种写法(转)

    . sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');或者 <if test ...

  9. idea将maven项目打包成war包的方式,以及使用war包

    打包WAR过程 首先对项目进行编译.然后执行以下步骤: 单击下图红色方框处  在IDEA右侧出现maven project选项  单击maven project选项,出现Spring MVC Basi ...

  10. 此地址使用了一个通常用于网络浏览以外目的的端口。出于安全原因,Firefox 取消了该请求。

    火狐无法访问本机IIS部署的网站,弹出:此地址使用了一个通常用于网络浏览以外目的的端口.出于安全原因,Firefox 取消了该请求 的解决办法 关于火狐浏览器访问本机IIS部署的网站弹出"此 ...