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

边栏推荐
- Oracle insert single quotation mark
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- Dotween plug-in
- Redis的数据结构
- Lua framwrok framework starts
- Cesium service deployment, and import and display local 3dfiles data
- oracle 插入单引号
- Basic operation and process control 2
- Golang url的编码和解码
- Zohocrm deluge function application time verification
猜你喜欢
Some understandings of 3dfiles
How to establish rectangular coordinate system in space
Transplantation of freetype Library
十六进制编码简介
An intern's journey to cnosdb
C language - Introduction - essence Edition - take you into programming (I)
Free use until 2015 -- viz artist multi touch plug-in package
Flex flexible box layout
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
【更新中】微信小程序学习笔记_3
随机推荐
YOLO系列 --- xml2txt脚本
Maxcompute string splitting function -split_ PART
Idea dereference display effect
[usaco12mar]cows in a skyscraper g (state compression DP)
Use filechannel to copy files
Luaframwrok handles resource updates
【K&R】中文第二版 个人题解 Chapter1
Classes and objects
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
MAE
PostGIS space function
Demonstration of plug-in use of ventuz basic series
Initial unity
Haproxy+kept cluster setup 02
Golang json格式和结构体相互转换
Kwai 20200412 recruitment
LinkList
P2704 [NOI2001] 炮兵阵地(状压dp)
the installer has encountered an unexpected error installing this package
【音视频】ijkplayer错误码