当前位置:网站首页>P1596 [USACO10OCT]Lake Counting S
P1596 [USACO10OCT]Lake Counting S
2022-07-03 08:14:00 【暴揍键盘的程序猿】
P1596 [USACO10OCT]Lake Counting S
# [USACO10OCT]Lake Counting S
## 题目描述
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.
由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个NxM(1<=N<=100;1<=M<=100)网格图表示。每个网格中有水('W') 或是旱地('.')。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。
## 输入格式
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.
第1行:两个空格隔开的整数:N 和 M 第2行到第N+1行:每行M个字符,每个字符是'W'或'.',它们表示网格图中的一排。字符之间没有空格。
## 输出格式
Line 1: The number of ponds in Farmer John's field.
一行:水坑的数量
## 样例 #1
### 样例输入 #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.
```
### 样例输出 #1
```
3
```
## 提示
OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.
【思路】
直接套dfs模板, 稍微修改即可通过。
【AC代码】
#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]=='.';//被搜索过
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;//判断边界条件
ch[z][p]='.',dfs(z,p);//符合条件,将ch[z][p]设为被搜索过,继续往下搜
}
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);//如果是水,记录水洼个数,并进行搜索
fwrite(ans);
return 0;
}调了半天调不出来干脆换了一种判断方式,然后就过了。

边栏推荐
猜你喜欢

超限黑客认知

L'installateur a été installé avec une erreur inattendue

【cocos creator】点击按钮切换界面

Wechat applet taro learning record

Three characteristics

Pulitzer Prize in the field of information graphics - malofiej Award

MAE

CLion-Toolchains are not configured Configure Disable profile问题解决

Install cross compiler arm none liunx gnueabihf

Storage of data
随机推荐
Scite change background color
2020-12-12
链式长取值
什麼是定義?什麼是聲明?它們有何區別?
Golang 时间格式整理
Clip Related Script
Demonstration of plug-in use of ventuz basic series
Storage of data
Minimap plug-in
數據庫應用技術課程設計之商城管理系統
jupyter远程服务器配置以及服务器开机自启
JS regular case-
[cocos creator] Click the button to switch the interface
Wechat applet taro learning record
P2622 light off problem II (state compression search)
Transfinite hacker cognition
Go resolve ID card
Golang 中string和int类型相互转换
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
LwIP learning socket (application)