Lake Counting (POJ No.2386)
有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼?
***
*W*
***
/**
*进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 .
*每次进行深度优先搜索的时候就将链接的水坑换成了.
*进行的深度优先搜索的次数就是水坑数
*/
#include<stdio.h>
#include<string.h>
const int MAX=;
int N,M;
char filed[MAX][MAX]; //园子的构造
//现在的位置(x,y)
void dfs(int x,int y){
//将现在所在的位置替换为‘.’
filed[x][y]='.';
//循环遍历移动的8个方向
for(int dx=-;dx<=;dx++){
for(int dy=-;dy<=;dy++){
int nx=x+dx,ny=y+dy;
//判断(nx,ny是不是在园子内,以及是否有积水)
if(<=nx && nx<N && <=ny && ny<M && filed[nx][ny]=='W')
dfs(nx,ny);
}
}
return;
}
void solve(){
int res=;
for(int i=;i<N;i++)
for(int j=;j<M;j++){
if(filed[i][j]=='W'){
dfs(i,j); //从有积水的地方开始遍历,遍历的次数就是水坑的个数
res++;
}
}
printf("%d\n",res);
}
int main(){
while(scanf("%d%d",&N,&M)==){
getchar();
for(int i=;i<N;i++)
for(int j=;j<M;j++)
scanf("%c",&filed[i][j]);
solve();
}
return ;
}
Lake Counting (POJ No.2386)的更多相关文章
- Lake Counting(poj 2386)
题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...
- Lak3 Counting(POJ No.2386)
问题描述: 有个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.求出园子里总共有多少水洼. N, M <= 100 输入例: : 问题分析: 八连通即:上.左上.左,左下,下 ...
- DFS----Lake Counting (poj 2386)
Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...
- 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(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- 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 ...
随机推荐
- [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- JSP错题纠错
A:判断学员是否手动安装过Tomcat(练习熟练度) B:使学员了解Tomcat的运行过程 ,浏览器向Web服务器发送请求,Web站点处理请求后,把处理后的结果响应给浏览器 C:Tomcat作为Web ...
- FileUtils.copyDirectory without .SVN
方法1:列出所有文件逐个筛选: File selectedFolder = new File(path); // path to folder to list final IOFileFilter d ...
- MyBatis一次执行多条SQL语句
MyBatis一次执行多条SQL语句 有个常见的场景:删除用户的时候需要先删除用户的外键关联数据,否则会触发规则报错. 解决办法不外乎有三个:1.多条sql分批执行:2.存储过程或函数调用:3.sql ...
- .net DataTable 正确排序姿势
关于dataTable中根据列排序正确姿势做个随笔,方便查阅 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Ad ...
- js简单排序
简单的排序功能 HTML代码: <body> <input id="btn1" type="button" value="排序&qu ...
- JS如何判断IE和火狐与Chrome浏览器
var isIE=navigator.userAgent.toUpperCase().indexOf("MSIE")?true:false; 类似的可以写var isFirefox ...
- iOS开发~interface Builder(简称 IB) 界面构建器
1.interface Builder 设置界面 1.1 是什么? 一个可视化的界面编辑工具软件,在xcode4之后整合到了xcode中 1.2 作用? 通过可视化的界面设置,能够少写或不写代码而完成 ...