Lake Counting

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53301   Accepted: 26062

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.

题意:W代表水塘,. 代表土地,问一共有多少个联通的水塘
 
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int dir[][]={{,-},{,},{,},{-,},{-,},{,},{-,-},{,-}};
char a[][];
int n,m;
int check(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&a[x][y]=='W')
return ;
else
return ;
}
void dfs(int x,int y)
{
if(check(x,y)==)
return;
if(a[x][y]=='W')
a[x][y]='.';
for(int i=;i<;i++)
{
int dx,dy;
dx=x+dir[i][];
dy=y+dir[i][];
dfs(dx,dy);
}
}
int main()
{
cin>>n>>m;
int cnt=;
for(int i=;i<n;i++)
cin>>a[i];
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='W')
{
dfs(i,j);
cnt++;
}
}
}
cout<<cnt<<endl;
return ;
}

最新文章

  1. web程序的路径笔记
  2. 8.1 EntityTypeConfiguration Class in Code-First【Code First系列】
  3. HTML5离线存储原理
  4. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f834824f\75776659\xxx.dll”--“拒绝访问。 ”
  5. C#继承基本控件实现自定义控件
  6. 【转】mac os x系统上Android开发环境的搭建
  7. dubbo管理控制台安装和使用
  8. SqlTransaction的解析
  9. 2014.8.18for循环
  10. .net core中加载lua脚本的类库: MoonSharp
  11. 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
  12. DotNetCore跨平台~服务总线_事件总线的重新设计
  13. masm中list文件和宏的一些常用编译调试查看方法
  14. C语言关键字register、extern、static、一些总结,及项目中使用的心得
  15. [2019.03.21]LF, CR, CRLF and LFCR(?)
  16. react-native flatlist 上拉加载onEndReached方法频繁触发的问题
  17. Python开发 基础篇
  18. Oracle部署安装
  19. linux(centos 7) 下安装nodejs
  20. 【论文阅读】ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices

热门文章

  1. 【原】cookie和session的区别
  2. Java面向对象编程 -1.3
  3. pexpect &amp;&amp;pxssh
  4. CSS - 背景半透明
  5. Python中神秘的-5到256
  6. ubuntu13.10安装tomcat
  7. OA:办公自动化———笔记一
  8. Ionic3记录之核心代码分析
  9. Lora笔记
  10. JS - 处理浏览器兼容之 event