来之不易的2017第一发ac

http://poj.org/problem?id=2386

Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31474   Accepted: 15724

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

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

 
 
import java.util.Scanner;

public class Main{
int n,m;
static int N = 105, M =105;
static char field[][] = new char[N][M];
static int res = 0; static void dfs(int x, int y) {
field[x][y] = '.';
for(int dx=-1;dx<=1;dx++)
for(int dy=-1;dy<=1;dy++)
{
int nx=x+dx;
int ny=y+dy;
if(nx>=0&&nx<N&&ny>=0&&ny<M&&(field[nx][ny]=='W'))
dfs(nx,ny);
} return;
} public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
N=cin.nextInt();
M=cin.nextInt();
String s;
for(int i=0;i<N;i++){
s = cin.next();
for(int j=0;j<M;j++)
field[i][j]=s.charAt(j);
} for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(field[i][j]=='W'){
dfs(i,j);
res++;
}
}
}
System.out.println(res);
cin.close();
}
}

POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)的更多相关文章

  1. [POJ 2386] Lake Counting(DFS)

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

  2. Lake Counting(DFS连通图)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  3. POJ 2386 Lake Counting DFS水水

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

  4. Lake Counting (DFS)

    N*M的园子,雨后积起了水.八连通的积水背认为是连接在一起的.请求出园子里总共有多少水洼? dfs(Depth-First  Search)  八个方向的简单搜索.... 深度优先搜索从最开始的状态出 ...

  5. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  6. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  7. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  8. POJ:2386 Lake Counting(dfs)

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

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

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

随机推荐

  1. Android数据加密之Aes加密

    前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...

  2. react+redux教程(六)redux服务端渲染流程

    今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...

  3. 如何重新注册VMware Update Manager(VUM)至vCenter Server中

    在VMware的vSphere化境中,VUM的角色相当于Windows 环境中的WSUS(Windows 更新服务器),可以批量,自动化的完成所管辖ESXi主机的大版本迁移,小版本升级的任务,深受管理 ...

  4. 深入理解java中的ArrayList和LinkedList

    杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...

  5. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  6. Objective-C集合总结

    Objective-C里面的集合主要包括:NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionar ...

  7. Android 的进程和线程

    进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...

  8. 在docker容器中vi指令找不到

    在使用docker容器时,有时候里边没有安装vi,敲vi命令时提示说:vi: command not found,这个时候就需要安装vi,可是当你敲apt-get install vi命令时,提示: ...

  9. SPI 2分频MOSI实现

    module spi_25M(input clk,input rst_n,output reg sdin,output reg sclk,output reg cs);reg [7:0]cnt;reg ...

  10. 在Word中输入数学公式

    office官方说明:https://support.office.com/en-us/article/Linear-format-equations-and-Math-AutoCorrect-in- ...