Lake Counting

Description

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.

Input

* 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.

Output

* Line 1: The number of ponds in Farmer John's field.

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

Java AC 代码:
 import java.util.Scanner;

 public class Main {
static int n,m,ans;
static char [][]filed = new char[105][105];
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
n = cin.nextInt();
m = cin.nextInt();
String s;
for(int i = 1;i <= n;i++) {
s = cin.next();
for(int j = 1;j <= m;j++) {
filed[i][j] = s.charAt(j - 1);
}
}
Slove();
System.out.println(ans);
}
} private static void Slove() {
ans = 0;
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= m;j++) {
if(filed[i][j] == 'W') {
DFS(i,j);
ans++;
}
}
}
} private static void DFS(int x,int y) {
filed[x][y] = '.';
for(int i = -1;i <= 1;i++) {
for(int j = -1;j <= 1;j++) {
int nx = x + i;
int ny = y + j;
if(filed[nx][ny] == 'W') {
DFS(nx,ny);
}
}
}
}
}

[POJ 2386] Lake Counting(DFS)的更多相关文章

  1. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  2. POJ:2386 Lake Counting(dfs)

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

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

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

  4. POJ 2386 Lake Counting(深搜)

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

  5. POJ 2386 Lake Counting

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

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

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

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

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

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

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

随机推荐

  1. jQuery属性--attr(name|properties|key,value|fn)和removeAttr(name)

       attr(name|properties|key,value|fn) 概述     设置或返回被选元素的属性值 参数 key,function(index, attr)  1:属性名称:2:返回 ...

  2. rest-framework 序列化格式Restful API设计规范

    理解RESTful架构 Restful API设计指南 理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式 ...

  3. VM VirtualBox 全屏模式 && 自动缩放模式 相互切换

    [1]自动缩放模式 热键Host + C 偶然一次机会,把虚拟机切换为了自动缩放模式,如下图: 想要再切换为全屏模式,发现不知如何操作,后来折腾了一会儿,切换成功.以此备录一下. [2]切换为全屏模式 ...

  4. QT构建窗体(父窗体传为野指针)异常案例

    [1]源码 工作中,时常会遇到各种各样的异常场景,有些异常场景很常见,必要备录,以防再犯. 分享本案例为:QT创建窗体时parent父窗体传野指针引起异常. 本案例源码如下: 1.1 默认新建一个QT ...

  5. 基于FPGA摄像头图像采集显示系统

    本系统主要由FPGA主控模块.图像采集模块.图像存储模块以及图像显示模块等模块组成.其中图像采集模块选择OV7670摄像头模块,完成对视频图像的采集和解码功能,并以RGB565标准输出RGB 5:6: ...

  6. flask模板应用-消息闪现(flash())

    消息闪现 flask提供了一个非常有用的flash()函数,它可以用来“闪现”需要提示给用户的消息,比如当用户登录成功后显示“欢迎回来!”.在视图函数调用flash()函数,传入消息内容,flash( ...

  7. Linux基础命令---arping

    arping arping指令用于发送arp请求到一个相邻的主机,在指定网卡上发送arp请求指定地址,源地址使用-s指定.该指令可以直径ping MAC地址,找出哪些地址被哪些电脑使用了. 此命令的适 ...

  8. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  9. Bluetooth_FTP_SPEC: 蓝牙FTP介绍

    FTP(Bluetooth File Transfer Profile) defines howfolders and files on a server device can be browsed ...

  10. Django框架----权限管理(设计分析以及具体细节)

    说起权限我们大家都知道,不一样的角色会有不一样的权限.比如就像学生管理系统一样,管理员,老师,学生之间的权限都是不一样的,那么展示的页面也是不一样的.所以,我们现在来看看具体操作. 目标:生成一个独立 ...