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

边栏推荐
- Are you still watching the weather forecast on TV?
- A tunnel to all ports of the server
- Transplantation of tslib Library
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
- Classes and objects
- 十六进制编码简介
- YOLO系列 --- xml2txt脚本
- String class
- haproxy+keepalived搭建01
- P1896 [SCOI2005] 互不侵犯(状压dp)
猜你喜欢
Oracle queries grouped by time
数据的存储
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
Get to know unity2 for the first time
Getting started with minicom
the installer has encountered an unexpected error installing this package
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
Haproxy+kept cluster setup 02
十六进制编码简介
Basic operation and process control
随机推荐
C语言-入门-精华版-带你走进编程(一)
Basic operation and process control
Use filechannel to copy files
链式长取值
Golang 中string和int类型相互转换
【cocos creator】点击按钮切换界面
2020-12-12
JS common basic case sorting (continuous update)
Wechat applet taro learning record
數據庫應用技術課程設計之商城管理系統
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
idea取消引用顯示效果
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
PIP uses image website to solve the problem of slow network speed
Initial unity
Unity change default editor
Iterm2 setting
VMware virtual machine configuration static IP
Get to know unity2 for the first time
C#课程设计之学生教务管理系统