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

边栏推荐
猜你喜欢

jupyter远程服务器配置以及服务器开机自启

Shader foundation 01

How to establish rectangular coordinate system in space

Three characteristics
![[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

WPF:解决MaterialDesign:DialogHost 无法关闭问题

数据的存储

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

Xlua task list youyou

Ventuz Foundation Series "one step at the door"
随机推荐
JS to implement publish and subscribe
【更新中】微信小程序学习笔记_3
Unity performance optimization
Conversion between string and int types in golang
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
Lua hot update basic grammar
L'installateur a été installé avec une erreur inattendue
使用 FileChannel 进行文件的复制拷贝
Pulitzer Prize in the field of information graphics - malofiej Award
Golang 字符串分割,替换和截取
P2622 light off problem II (state compression search)
Viz artist advanced script video tutorial -- stringmap use and vertex operation
What is BFC?
P2622 关灯问题II(状态压缩 搜索)
數據庫應用技術課程設計之商城管理系統
Ue5 opencv plug-in use
Abstract classes and interfaces
Some understandings of 3dfiles
简易入手《SOM神经网络》的本质与原理
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“