总时间限制: 
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的更多相关文章

  1. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  2. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  3. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  4. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  5. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  6. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  7. POJ 之2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20003   Accepted: 10063 D ...

  8. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  9. 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). ...

  10. POJ No.2386 Lake Counting

    题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...

随机推荐

  1. 从零开始教你封装自己的vue组件

    组件(component)是vue.js最强大的功能之一,它可以实现功能的复用,以及对其他逻辑的解耦.但经过一段时间的使用,我发现自己并没有在业务中发挥出组件的最大价值.相信很多刚开始使用vue的朋友 ...

  2. MySql技术内幕之MySQL入门(2)

    MySql技术内幕之MySQL入门(2) 接上一篇. mysql> source create_member.sql; # 创建member表 Query OK, 0 rows affected ...

  3. codeblocks无法编译的问题

    (题外话:网上垃圾资源太多,良心推荐下载 codeblocks的码农们,别TM用DevC++,百度搜索100个不用devc++的理由加上我自己亲身经历!!!) https://jingyan.baid ...

  4. hibernate的操作Blob和Clob类型数据(笔记)

  5. lua中的require机制

    lua中的require机制 为了方便代码管理,通常会把lua代码分成不同的模块,然后在通过require函数把它们加载进来.现在看看lua的require的处理流程.1.require机制相关的数据 ...

  6. 面试题----寻找比一个N位数大的“下”一个数

    题目描述 写出一个算法,实现如下功能: 给定一个N位数字组成的数,找出比这个数大的由相同数字组成的下一个数 例如:如果数字为 25468, 则结果为25486 如果数字为 21765, 则结果为 25 ...

  7. Fibonacci Numbers

    Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  8. JavaScript instanceof 运算符深入剖析【转载】

    http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/   instanceof 运算符简介 在 JavaScript ...

  9. 可点击的icon按钮 无障碍 ARIA 可访问性

    最简单: <input type="image" src="email.png" width="14" height="14 ...

  10. 【译】Asp.Net Identity Cookies 格式化-中英对照版

    原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland I've been reached out ...