2386:Lake Counting-poj
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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.
- 输入
- * 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.
- 输出
- * Line 1: The number of ponds in Farmer John's field.
- 样例输入
-
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W. - 样例输出
-
3
- 提示
- OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
- 代码:递归 搜索
-
#include<stdio.h> int n,m;
char map[][];
int go[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};//方向数组,上下左右--八个方向
void dfs(int x,int y)
{
map[x][y]='.';//标记已经走过的节点
for(int i=;i<=;i++)
{
int nx=x+go[i-][];//搜索上下左右斜边八个方向
int ny=y+go[i-][];
if(nx>=&&nx<n&&ny>=&&ny<m&&map[nx][ny]=='W')//遇到w并且没有越界 就一直遍历下去
dfs(nx,ny);
} } int main(void)
{
int i,j,k;
while(scanf("%d%d",&n,&m)==)
{
getchar();
k=;
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
scanf("%c",&map[i][j]);
}
getchar();
}
for(i=; i<n; i++)
for(j=; j<m; j++)
if(map[i][j]=='W')
{
dfs(i,j);
k++;//每次找到一个水域,计数值增加1
}
printf("%d\n",k);
}
return ;
}
2386:Lake Counting-poj的更多相关文章
- 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 ...
- POJ 之2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20003 Accepted: 10063 D ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- 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). ...
- POJ No.2386 Lake Counting
题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...
随机推荐
- UVa 1608,Non-boring sequences
好诡异的一个题啊 紫书上关于从左边找还是从两边往中间找的讨论没有看懂,怎么一下就找到唯一的元素了(⊙_⊙?) 方法就是用的书上讲的方法,类似于uva 11572,不过这个题需要预处理存下两边的最近的相 ...
- jdk8 JAVA_OPTS
JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -Djava. ...
- Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程
事前准备 最近给游戏添加了苹果的内购,这一块的东西也是刚刚做完,总结一下,其实这里不管是游戏还是我们普通的App添加内购这一块的东西都是差不多的,多出来的部分就是我们Lua和OC的交互的部分,以前刚开 ...
- UNIX发展史(BSD,GNU,linux)
先前的一個理想 UNIX 系统自 1969 年 Ken Thompson 与 Dennis Ritchie 在美国贝尔电话实验室(Bell Telephone Laboratories)发展出雏形至今 ...
- javascript 之异常处理try catch finally--05
语法结构 try catch finally是ECMAScript-262 第三版提供异常处理机制的标准,语法结构如下: try{ //可能会发生的错误代码 } catch(error){ //错误处 ...
- PHP设计模式二:单例模式
一.什么是单例模式 作为对象的创建模式,单例模式确保某一个类只有一个实例,并且对外提供这个全局实例的访问入口.它不会 创建实例副本,而是会向单例类内部存储的实例返回一个引用. 二.PHP单例模式三要素 ...
- Golang访问Redis初体验
go语言的client在redis官网上有很多l客户端,个人感觉redigo使用起来更人性化,重要的是源代码结构很清晰,重要的是支持管道.发布和订阅.连接池等等,所以我选择redigo作为尝试. 1. ...
- Linux环境下网卡配置
DEVICE=eth0 HWADDR=08:00:27:0D:3C:F6 TYPE=Ethernet UUID=73ff4482-1baf-4c9b-b859-720ca92a704a ONBOOT= ...
- Can you solve this equation?
Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its sol ...
- NFS存储服务部署
第1章 NFS介绍 1.1 NFS服务内容的概述 □ RPC服务知识概念介绍说明,以及RPC服务存在价值(必须理解掌握) □ NFS服务工作原理讲解(必须理解掌握) □ NFS共享文件系统使用原理讲解 ...