poj-2386 lake counting(搜索题)
Time limit1000 ms
Memory limit65536 kB
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
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
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define PI 3.14159265358979323846264338327950 int N,M;
const int MAX_N=;
char field[MAX_N][MAX_N]; void dfs(int x,int y)
{
field[x][y]='.';
for(int dx=-;dx<=;dx++)
{
for(int dy=-;dy<=;dy++)
{
int nx=x+dx,ny=y+dy;
if(<=nx && nx<N && <=ny && ny<M && field[nx][ny]=='W')
dfs(nx,ny);
}
}
return ;
} void solve()
{
int res =;
for(int i=;i<N;i++)
{
for(int j=;j<M;j++)
{
if(field[i][j]=='W')
{
dfs(i,j);
res++;
}
}
}
printf("%d\n",res);
} int main()
{
cin>>N>>M;
for(int i=;i<N;i++)
for(int j=;j<M;j++)
cin>>field[i][j];
solve(); }
poj-2386 lake counting(搜索题)的更多相关文章
- POJ 2386 Lake Counting 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...
- POJ 2386 Lake Counting (水题,DFS)
题意:给定一个n*m的矩阵,让你判断有多少个连通块. 析:用DFS搜一下即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- POJ 2386 Lake Counting DFS水水
http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...
随机推荐
- python 6 循环
循环 要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 6 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+10000,直接写表 ...
- Yahoo!团队实践分享:网站性能优化的34条黄金守则
(一)内容 Yahoo!的Exceptional Performance团队为改善Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.最佳实 ...
- 《java学习二》并发编程
多线程创建方式 1.继承thread类,重写run方法 CreateThread createThread = new CreateThread(); ------createThread ...
- scp 可以在 2个 linux 主机间复制文件
Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...
- HandlerMapping执行过程。。。
1.web.xml DispatcherServlet 类 寻址 doDispatch() 2.getHandler(requset) 点击,进入 3.HandlerMapping hm=xxxxxx ...
- logback整合Logstash
1.依赖 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logsta ...
- 【Linux】Ubuntu配置zshell&oh-my-zsh
zshell:https://archive.codeplex.com/?p=shell oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh 终极 ...
- HTML5 JSDOM
1,HTML5 新语义化标签 - nav -- 表示导航 - header -- 表示页眉 -- 头部 - section -- 表示区块 -- 类似于div - main -- 文档主要内容 - a ...
- 学JS的书籍
1.JavaScript DOM 编程艺术 [说明] 这本书最大的特点就是简明易懂,循序渐进,适合初学者,非常容易上手. 计划:三天读完 读书总结:待写 2.Javascript权威指南 特点是权威. ...
- 实现memcopy函数
实现memcopy函数: void * memcpy(void *dest, const void *src, unsigned int count); { if ((src == NULL) || ...