POJ No.2386【B007】
【B007】Lake Counting【难度B】——————————————————————————————————————————
【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.
【Source】
【分析】
不要被那么一大串英文吓到,这就是一个统计八连快。。。。。。果断DFS,秒A。。。。。。
【代码】
#include<iostream>
using namespace std;
const int maxn=101;
const int maxm=101;
int n,m;
char field[maxn][maxm];
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,ny=y+dy;
if(0<=nx && nx<n && 0<=ny && ny<m && field[nx][ny]=='W') dfs(nx,ny);
}
}
return ;
}
int main()
{
int res=0;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>field[i][j];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(field[i][j]=='W')
{
dfs(i,j);
res++;
}
}
}
cout<<res;
return 0;
}
POJ No.2386【B007】的更多相关文章
- POJ No.3617【B008】
[B007]Best Cow Line[难度B]———————————————————————————————————————————————— [Description 支持原版从我做起!!! ...
- [POJ 1742] Coins 【DP】
题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...
- POJ 3040 Allowance【贪心】
POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...
- POJ 1017 Packets【贪心】
POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常 ...
- poj 1159 Palindrome 【LCS】
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...
- POJ - 3414 Pots 【BFS】
题目链接 http://poj.org/problem?id=3414 题意 给出两个杯子 容量分别为 A B 然后给出C 是目标容量 有三种操作 1 将一个杯子装满 2.将一个杯子全都倒掉 3.将一 ...
- POJ 2442 Sequence【堆】
题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加.能得到n^m个不同的结果.要求输出当中前n项. 建立一个以n元数组为底 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 3304 Segments【叉积】
题意:有n条线段,问有没有一条直线使得所有线段在这条直线上的投影至少有一个共同点. 思路:逆向思维,很明显这个问题可以转化为是否有一条直线穿过所有线段,若有,问题要求的直线与该直线垂直,并且公共点为垂 ...
随机推荐
- jQuery学习过程问题笔记
1. jQuery中,$('selector').click(function(){})和用bind绑定:$('selector').bind('click',function(){})有什么区别? ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- vim 添加php自动补全 并格式化代码
自动补全,修改/etc/vimrc的配置 vim /etc/vimrc 添加: filetype plugin on autocmd FileType php set omnifunc=phpcomp ...
- visual studio 2013 中配置OpenCV2.4.13 姿势
首先在path中添加 “C:\OpenCV\opencv\build\x64\vc12\bin” (地址随实际变化) 注:添加的都是*86不使用*64 在visualstudio 2013中配置 ...
- erlang 分布式数据库Mnesia 实现及应用
先推荐一篇:mnesia源码分析(yufeng) - linear hash ETS/DETS/mnesia 都使用了linear hash算法 http://en.wikipedia.org ...
- SSH使用详解
一.SSH基础 (1)什么是SSH? 传统的网络服务程序,如:ftp.pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令和数据.而且 ...
- 转:POI操作Excel导出
package com.rd.lh.util.excel; import java.beans.PropertyDescriptor; import java.io.FileOutputStream; ...
- ajax循环读取json多维数组
test.json: { "one": [ { "name": "黑默丁格", "car": "鲁LLL608 ...
- SecureCRT在远程主机和本地之间传输文件
概述 Linux系统不像Window系统有清晰的操作页面,数据能Copy下来,远程Linux的数据,就牵扯到本地数据与远程服务器数据交换的问题.Linux远程攻击SecureCrt提供了2种方式,可以 ...
- Jenkins配置自动发送邮件,成功!
Jenkins自动发送邮件配置: 打开"系统管理"--"系统设置" 在"Jenkins Location"设置系统管理员地址(重要:不能省略 ...