DFS----Lake Counting (poj 2386)
Lake Counting(POJ No.2386)
Description
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
- 1 #include <iostream>
- 2 using namespace std;
- 3 int N,M;
- 4 //int res=0;
- 5 const int MAX_N=1000;
- 6 const int MAX_M=1000;
- 7 char field[MAX_N][MAX_M];
- 8 void dfs(int x,int y)
- 9 {
- 10 field[x][y]='.';
- 11 for(int dx=-1;dx<=1;dx++)
- 12 {
- 13 for(int dy=-1;dy<=1;dy++)
- 14 {
- 15 int nx=dx+x,ny=dy+y;
- 16 if(nx>=0&&nx<N&&ny>=0&&ny<M&&field[nx][ny]=='W')
- 17 dfs(nx,ny);
- 18 }
- 19 }
- 20 return;
- 21 }
- 22 void solve()
- 23 {
- 24 int res=0;
- 25 for(int i=0;i<N;i++) {
- 26 for(int j=0;j<M;j++){
- 27 if(field[i][j]=='W') {
- 28 dfs(i, j);
- 29 res++;
- 30 }
- 31 }
- 32 }
- 33 cout<<res<<endl;
- 34 }
- 35 int main() {
- 36 cin>>N>>M;
- 37 for(int x=0;x<N;x++)
- 38 {
- 39 for(int y=0;y<M;y++)
- 40 {
- 41 cin>>field[x][y];
- 42 }
- 43 // printf("\n");
- 44 }
- 45 solve();
- 46 //cout<<res<<endl;
- 47 return 0;
- 48 }
DFS----Lake Counting (poj 2386)的更多相关文章
- Lake Counting(poj 2386)
题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...
- DFS:Lake Counting(POJ 2386)
好吧前几天一直没更新博客,主要是更新博客的确是要耗费一点精力 北大教你数水坑 最近更新博客可能就是一点旧的东西和一些水题,主要是最近对汇编感兴趣了嘻嘻嘻 这一题挺简单的,没什么难度,简单深搜 #inc ...
- Lake Counting (POJ No.2386)
有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼? *** *W* *** /** *进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 ...
- [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深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
- 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 ...
随机推荐
- echart 圆滑初始化化
圆滑:主题下载对应主题js引入后注入对应名称参数方可使用主题 初始化:tab点击的时候初始化图表涉及到tab切换到的需要延迟加载否则默认宽度为100px 1.创建macarons.js文件 2.页面添 ...
- oracle.exe 内存占用过大
现象: 明明各个schema 占用的磁盘空间都不大. oracle.exe 却占用了差不多 3G 的内存. 解决: 查了google,各种英文关键字没有找到原因. 最后,中文检索到了. https:/ ...
- HTML第五章总结
A Chapter for <img> 前言 这一章讲了 Web 图片 format 的各自的优缺点和elements of 's attributes. Section1:Q1:How ...
- Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。
需要用到的backed tech stack: Node: JavaScript on the server/backend. That's basically what it is, but mor ...
- android -------- 错误Attribute application@allowBackup value=(true) from AndroidManifest.xml
开发中遇到一个问题,运行项目时,出现了一个这如下这样的问题 问题: Manifest merger failed : Attribute application@allowBackup value=( ...
- android -------- 我创建的第一个 NDKDmeo 案例
前面的NDK是弄的官方的,自己弄了一下,弄让他运行起来,今天来简单的写一个. 我是在Eclipse中开发的,创建一个NDKDemo项目,然后如下图: 在项目上–>右键–>Android T ...
- HTML页面加载完毕后运行的js
Js方法:<script type=”text/javascript”> window.onload=function (){ var userName=”xiaoming”; alert ...
- Xpath做数据解析
xpath是一个路径表达式, xpath学习 (1)xpath节点 在XPath中,有七种类型的节点:元素,属性,文本,命名空间,处理指令,注释以及文档节点:XML文档是被作为节点树来对待的.树的根被 ...
- 学习Spring Security OAuth认证(一)-授权码模式
一.环境 spring boot+spring security+idea+maven+mybatis 主要是spring security 二.依赖 <dependency> <g ...
- SQL - 数据定义
SQL 的数据定义功能包括模式定义.表定义.视图和索引的定义: 操作对象 操作方式 创建 删除 修改 模式 create schema drop schema 表 create table d ...