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. 【转】推荐4个不错的Python自动化测试框架

    之前,开发团队接手一个项目并开始开发时,除了项目模块的实际开发之外,他们不得不为这个项目构建一个自动化测试框架.一个测试框架应该具有最佳的测试用例.假设(assumptions).脚本和技术来运行每一 ...

  2. Explorer Bo (思维 + 树链剖分)

    题意:求用最少的链覆盖所有的边用最少的总链长度. 思路:为了使得使用的链最少,我们可以知道使用的数量应该是(子叶 + 1)/ 2. 画图可知:当节点下的边数是偶数时,为了将该父节点上的边给连接上,所以 ...

  3. 2017-2018-1 20155228 《信息安全系统设计基础》第六周学习总结&课下作业

    20155228 2017-2018-1 <信息安全系统设计基础>第六周学习总结&课下作业 教材学习内容总结 异常及其种类 异常可以分为四类:中断(interrupt) ,陷阱(t ...

  4. Runtime(IV) - 序列化与反序列化

    准备条件 父类 Biology Biology.h #import <Foundation/Foundation.h> @interface Biology : NSObject { NS ...

  5. 原型设计模式 Prototype

    参考1 http://www.cnblogs.com/libingql/p/3633377.html http://www.cnblogs.com/promise-7/archive/2012/06/ ...

  6. 取n到m行

    取n到m行 . select top m * from tablename where id not in (select top n id from tablename order by id as ...

  7. File §1

    The Class of File, it can be seen as one document, also can be seen as list of documents. File  f = ...

  8. vs实现数据库数据迁移

    public ActionResult About() { List<ChangeData.Models.old.adsinfo> adsinfo_new = new List<Mo ...

  9. ad 原件布局布线基本规则

    一.原件布局基本规则 1.按照电路模块进行布局,电路中的元件应该采用集中就近原则,同时数字电路和模拟电路分开: 2.定位孔.标准孔等周围1.27mm内不得贴元器件,安装孔周围3.5mm不得特装元件 3 ...

  10. 初探AngularJs框架(一)

    一.需要准备的环境 Nodejs:https://nodejs.org/en/download/ Python:https://www.python.org/downloads/release/pyt ...