poj2386 Lake Counting(简单DFS)
viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=1562
- ----------------------------------------------------------------------------------------------------------------------------------------------------------
- ----------------------------------------------------------------------------------------------------------------------------------------------------------
Description
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
* 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
代码例如以下:
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #include <cstring>
- #define TM 100+17
- int N, M;
- char map[TM][TM];
- bool vis[TM][TM];
- int xx[8]={0,1,1,1,0,-1,-1,-1};
- int yy[8]={1,1,0,-1,-1,-1,0,1};
- void DFS(int x, int y)
- {
- vis[x][y] = true;
- for(int i = 0; i < 8; i++)
- {
- int dx = x+xx[i];
- int dy = y+yy[i];
- if(dx>=0&&dx<N&&dy>=0&&dy<M&&!vis[dx][dy]&&map[dx][dy] == 'W')
- {
- vis[dx][dy] = true;
- DFS(dx,dy);
- }
- }
- }
- int main()
- {
- int i, j;
- while(cin>>N>>M)
- {
- int count = 0;
- memset(vis,false,sizeof(vis));
- for(i = 0; i< N; i++)
- {
- cin>>map[i];
- }
- for(i = 0; i < N; i++)
- {
- for(j = 0; j < M; j++)
- {
- if(map[i][j] == 'W' && !vis[i][j])
- {
- count++;
- DFS(i,j);
- }
- }
- }
- cout<<count<<endl;
- }
- return 0;
- }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj2386 Lake Counting(简单DFS)的更多相关文章
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- POJ2386 Lake Counting 【DFS】
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20782 Accepted: 10473 D ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- poj-2386 lake counting(搜索题)
Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
随机推荐
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)
In this Document Abstract History Details _afrLoop=2234450430619177&id=1537521.1&dis ...
- Eclipse中的Maven项目报Unbound classpath variable错误
今天更新了最新版的Eclipse luna 4.4版本号,svn更新了项目后,系统一直报错,经查看在Problems窗体中发现一堆错误.提演示样例如以下: Unbound classpath ...
- "ScrollView can host only one direct child"问题解决了
1. 问题叙述性说明: (请注意以下几点大胆). ScrollView作为顶层view时报错,直接导致apk崩溃.具体错误信息例如以下: 12-21 09:12:15.150: D/AndroidRu ...
- C#的百度地图开发(四)前端显示与定位
原文:C#的百度地图开发(四)前端显示与定位 有了这些定位信息,那要如何在前端的页面上显示出来呢?这需要用到百度地图的JavaScript的API.下面是示例代码. 前端代码 <%@ Page ...
- Linux档案种类与扩展名(2013.09.03)
档案种类: 正规档案(regular file ): 第一个字符为 [ -],例如 [-rwxrwxrwx ].另外,依照档案的内容,又大略可以分为: 纯文本档(ASCII) 二 ...
- 用Tomcat和Eclipse开发Servlet程序
1. 安装eclipse 1). 在官网上直接下载Eclipse IDE for Java EE Developers,解压即可: 2. eclipse安装tomcat插件: 1). 在http:// ...
- OpenStack安装与配置2
第二部分 OpenStack安装与配置 一.引言 本章内容讲解如何在3台物理机上搭建最小化云平台,这3台机器分为称为Server1.Server2和Client1,之后的各章也是如此.Server ...
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十三)
今天我们实验libvirt提供的快照功能,快照可以用于系统恢复,防止安装了某些软件或中病毒等情况导致系统损毁的情况. 一.快照类型 1) 磁盘快照 内部的:快照驻留在原来的镜像文件内部 ...
- onmouseover 执行 ToolTip 控件
Tooltip控件是一个简单,但非常有用的控件.它能够为我们的软件提供非常漂亮的提示信息,提高软件的可用性,给用户比较好的体验.假设现在有两个按钮,一个用来预览吊线世系图,一个用来预览行转.为了保持按 ...