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

边栏推荐
- Base64编码简介
- Golang json格式和结构体相互转换
- I want to do large screen data visualization application feature analysis
- [cocos creator] get the resource UUID
- Lua framwrok framework starts
- Basic operation and process control
- Wechat applet taro learning record
- haproxy+keepalived搭建01
- My touch screen production "brief history" 1
- Idea unreference Display Effect
猜你喜欢

Get to know unity2 for the first time

十六进制编码简介

freetype库的移植

ArrayList

Iterm2 setting

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

Haproxy+kept cluster setup 02

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

*p++、*++p、++*p、(*p)++

Minimap plug-in
随机推荐
Go resolve ID card
Yolo series --- xml2txt script
Golang time format sorting
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
Transfinite hacker cognition
Map的实现类的顺序性
jupyter远程服务器配置以及服务器开机自启
Initial unity
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
About Wireshark's unsuccessful installation of npcap
Editor Extensions
【cocos creator】点击按钮切换界面
Maxcompute string splitting function -split_ PART
I want to do large screen data visualization application feature analysis
Haproxy+kept cluster setup 02
STM32F103 SPI (pit Diary)
十六进制编码简介
swagger文档配置
Shader foundation 01