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

边栏推荐
- Xlua task list youyou
- Are you still watching the weather forecast on TV?
- Register keyword
- Youyou1 of xlua knapsack system
- P2704 [noi2001] artillery position (shape pressure DP)
- Idea unreference Display Effect
- Wpf: solve the problem that materialdesign:dialoghost cannot be closed
- Worldview satellite remote sensing image data / meter resolution remote sensing image
- [cocos creator] Click the button to switch the interface
- About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
猜你喜欢

Wechat applet taro learning record

Get to know unity2 for the first time

Oracle queries grouped by time

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

Lua framwrok framework starts

the installer has encountered an unexpected error installing this package

VMware virtual machine configuration static IP

How to establish rectangular coordinate system in space
![[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)](/img/de/1c75b5e7ed79aca47462de365428a7.jpg)
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
随机推荐
Basic operation and process control
Register keyword
VMware virtual machine configuration static IP
P2622 light off problem II (state compression search)
String class
Classes and objects
Abstract classes and interfaces
One dimensional array two dimensional array (sort Max insert sort)
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Are you still watching the weather forecast on TV?
CLion-Toolchains are not configured Configure Disable profile问题解决
Three characteristics
the installer has encountered an unexpected error installing this package
Compilation error: "not in executable format: file format not recognized"“
C语言-入门-精华版-带你走进编程(一)
C#课程设计之员工信息管理系统
P1896 [SCOI2005] 互不侵犯(状压dp)
[usaco12mar]cows in a skyscraper g (state compression DP)
jupyter远程服务器配置以及服务器开机自启
Base64编码简介