当前位置:网站首页>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 .

边栏推荐
- What does (+) in Oracle mean
- Use of ue5 QRcode plug-in
- the installer has encountered an unexpected error installing this package
- Generate video using clipout in viz engine
- Exe file running window embedding QT window
- Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
- the installer has encountered an unexpected error installing this package
- IP production stream is so close to me
- Shader foundation 01
- matlab神经网络所有传递函数(激活函数)公式详解
猜你喜欢

Transfinite hacker cognition

Base64 and base64url
![[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel](/img/51/04f5a9dbd03438fbdf25545a81b7ba.jpg)
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel

Unity2019_ Lighting system

Ue5 opencv plug-in use

the installer has encountered an unexpected error installing this package

Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données

璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型

Transplantation of freetype Library

Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
随机推荐
Clip Related Script
idea取消引用顯示效果
My touch screen production "brief history" 2
十六进制编码简介
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
梯度下降法求解BP神经网络的简单Demo
Basic operation and process control 2
Transfinite hacker cognition
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
【K&R】中文第二版 个人题解 Chapter1
Get to know unity2 for the first time
链式长取值
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
regular expression
Abstract classes and interfaces
I want to do large screen data visualization application feature analysis
详解sizeof、strlen、指针和数组等组合题
Idea dereference display effect
Ilruntime learning - start from scratch
UE4 call DLL