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

Sample Input

W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W. Sample Output

题意:

  W表示水洼;.表示空地,八个方向连起来的水洼也算是一个,问有多少水洼。

思路:

  简单的搜索题目,这里用深搜实现。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
char map[][];//用来存储图的信息 /*标记是否搜索过,本题目没有使用,因为是一次访问,直接将访问过的修改即可*/
int logo[][]; int m,n,sum; /*表示八个方向,四个方向时,将后面四组删掉就可以了*/
int dir[][]= {,, ,-, ,, -,, ,, ,-, -,, -,-}; void DFS(int x,int y)
{
if(x>=&&y>=&&x<n&&y<m)//这里是判断是否越界,根据题目要求改写
{
if(map[x][y]=='W')//如果符合条件就继续递归。
{
map[x][y]='.';//标记为‘.’防止多次访问
for(int i=; i<; i++)//因为八个方向,所以循环八次。
DFS(x+dir[i][],y+dir[i][]);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
sum=;
memset(logo,,sizeof(map));
getchar();
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
cin>>map[i][j];
}
getchar();
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
if(map[i][j]=='W')
{
DFS(i,j);
sum++;//计数
}
}
cout<<sum<<endl;
}
}

POJ 2386 Lake Counting (简单深搜)的更多相关文章

  1. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  2. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  3. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  4. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  5. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  6. [POJ 2386] Lake Counting(DFS)

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

  7. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  8. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  9. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

随机推荐

  1. openfire极限优化

    日志优化   默认是 用info 级别,最好不用openfire原生的打日志方式.   离线消息用存储不打回方式,不要用打回方式   xmpp.offline.type=store_and_drop ...

  2. 【转载】QT QTableView用法小结

    原始日期: 2016-08-16 09:28 来源:http://blog.csdn.net/wang_lichun/article/details/7805253 QTableView常用于实现数据 ...

  3. Redis客户端管理工具的安装及使用

    1.下载及安装 请到官网下载:www.treesoft.cn,要最新的版本treeNMS, window系统下载直接解压,就可以用了,免安装,免布署. 2.登录及连接参数配置 登录后,要配置连接参数信 ...

  4. WebSocket 结合 Nginx 实现域名及 WSS 协议访问

    简单了解一下 WebSocket 现在,很多网站为了实现推送技术,所用的技术都是轮询.轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTP请求,然后由服务器返回最新的数据给客户端的浏览器 ...

  5. OpenStack(企业私有云)万里长征第三步——调试成功

    一.前言 折腾了一两个月(中间有事耽搁了半个月),至今日基本调试成功OpenStack,现将中间的部分心得记录下来. 二.环境 使用的是devstack newton版.具体部署过程可以参考cloud ...

  6. Example004自动关闭的广告窗口

    <!-- 实例004自动关闭的广告窗口--> <!-- 3秒后关闭 --> <body onload="window.setTimeout('window.cl ...

  7. WCF学习——构建第二个WCF应用程序(五)

    一.创建数据服务 1.在“解决方案资源管理器”中,使用鼠标左键选中“WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 2.在“添加新项”对话框中,选择“Web”节点,然后选 ...

  8. struts2框架下的一个简单的ajax例子

    举个例子 jsp页面: <%@ page language="java" import="java.util.*" pageEncoding=" ...

  9. iOS多线程开发之GCD(中篇)

    前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...

  10. Vue.js学习笔记(二) - 常用模板语法

    本篇将在上一篇的基础上记录文本渲染.表达式.过滤器以及常用指令的简单用法. 一.文本渲染 Vue支持动态渲染文本,即在修改属性的同时,实时渲染文本内容.同时为了提高渲染效率,也支持只渲染一次,即第一次 ...