当前位置:网站首页>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;
}调了半天调不出来干脆换了一种判断方式,然后就过了。

边栏推荐
- Abstract classes and interfaces
- Multi traveling salesman problem -- overview of formula and solution process
- Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
- CLion-Toolchains are not configured Configure Disable profile问题解决
- E: 无法定位软件包 ros-melodic-desktop-full
- Are you still watching the weather forecast on TV?
- Transplantation of tslib Library
- Unity performance optimization
- ArrayList
- vcs import src < ros2. Repos failed
猜你喜欢
随机推荐
Classes and objects
Register keyword
Unity2019_ Natural ambient light_ Sky box
One dimensional array two dimensional array (sort Max insert sort)
Easy touch plug-in
超限黑客认知
Conversion between golang JSON format and structure
the installer has encountered an unexpected error installing this package
Conversion between string and int types in golang
2020-12-12
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
数据分析练习题
Iterm2 setting
使用 FileChannel 进行文件的复制拷贝
Base64编码简介
Yolo series --- xml2txt script
[cocos creator] get the resource UUID
2021-10-19
链式长取值
C language - Introduction - essence Edition - take you into programming (I)









