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