当前位置:网站首页>P1596 [USACO10OCT]Lake Counting S
P1596 [USACO10OCT]Lake Counting S
2022-07-03 08:17:00 【A program ape who beats the keyboard violently】
P1596 [USACO10OCT]Lake Counting S
# [USACO10OCT]Lake Counting S
## Title 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.
Due to the recent rainfall , The rain collected in different parts of Farmer John's field . We use one NxM(1<=N<=100;1<=M<=100) The grid diagram shows . There is water in each grid ('W') Or dry land ('.'). A grid is connected to eight grids around it , A set of connected grids is regarded as a puddle . John wanted to find out how many puddles had formed in his field . Give a schematic diagram of John's field , Determine how many puddles there are .
## Input format
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.
The first 1 That's ok : An integer separated by two spaces :N and M The first 2 Go to the first place N+1 That's ok : Each row M Characters , Each character is 'W' or '.', They represent a row in a grid diagram . There is no space between characters .
## Output format
Line 1: The number of ponds in Farmer John's field.
a line : Number of puddles
## Examples #1
### The sample input #1
```
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 #1
```
3
```
## Tips
OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.
【 Ideas 】
Direct set dfs Templates , Slightly modify You can pass .
【AC Code 】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e3+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int n,m,ans,a[N]={0,-1,-1,-1,0,0,1,1,1},b[N]={0,-1,0,1,-1,1,-1,0,1};
char ch[N][N];
void dfs(int x,int y)
{
ch[x][y]=='.';// Searched
int z,p;
for(int i=1;i<9;i++)
{
z=x+a[i],p=y+b[i];
if(z<1 or z>n or p<1 or p>m or ch[z][p]=='.')continue;// Judge boundary condition
ch[z][p]='.',dfs(z,p);// eligible , take ch[z][p] Set as searched , Keep searching
}
return;
}
signed main()
{
n=fread(),m=fread();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)cin>>ch[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(ch[i][j]=='W')ans++,dfs(i,j);// If it's water , Record the number of puddles , And search
fwrite(ans);
return 0;
}After adjusting for a long time, I can't adjust it. I just changed a way of judging , And then it passed .

边栏推荐
- Some understandings of 3dfiles
- Wechat applet taro learning record
- haproxy+keepalived搭建01
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- Generate video using clipout in viz engine
- Chain length value
- Mutual call between Lua and C #
- Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
- 【音视频】ijkplayer错误码
- P1896 [SCOI2005] 互不侵犯(状压dp)
猜你喜欢

Zohocrm deluge function application time verification

Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products

PostGIS space function

Base64编码简介

Pulitzer Prize in the field of information graphics - malofiej Award

C language - Introduction - essence Edition - take you into programming (I)

Mall management system of database application technology course design

the installer has encountered an unexpected error installing this package

Unity2019_ Lighting system

C course design employee information management system
随机推荐
Golang url的编码和解码
【更新中】微信小程序学习笔记_3
Cesium service deployment, and import and display local 3dfiles data
Unity change default editor
Unity one click AssetBundle
E: Unable to locate package ROS melody desktop full
An intern's journey to cnosdb
数据库应用技术课程设计之商城管理系统
Redis的数据结构
CLion-Toolchains are not configured Configure Disable profile问题解决
oracle中的 (+)是什么意思
KunlunBase MeetUP 等您来!
PostGIS space function
Golang 时间格式整理
P1596 [USACO10OCT]Lake Counting S
I want to do large screen data visualization application feature analysis
Golang string segmentation, substitution and interception
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Yolo series --- xml2txt script
一条通往服务器所有端口的隧道