[USACO10OCT] Lake Counting S
传送锚点:https://www.luogu.com.cn/problem/P1596
由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个 \(N\times M(1\leq N\leq 100, 1\leq M\leq 100)\) 的网格图表示。每个网格中有水(W) 或是旱地(.)。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。
输入第 \(1\) 行:两个空格隔开的整数:\(N\) 和 \(M\)。
第 \(2\) 行到第 \(N+1\) 行:每行 \(M\) 个字符,每个字符是 W 或 .,它们表示网格图中的一排。字符之间没有空格。
输出一行,表示水坑的数量。
题目描述
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.
输入格式
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.
输出格式
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.
思路
code
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m;//n为横向,m为纵向
const int maxn = 105;
int visited[maxn][maxn];//visited[i][j]为(坐标为(i,j))1代表访问过
char grid[maxn][maxn];//存储瓷砖内容
int res = 0;//统计水坑个数
int dx[8] = { -1,-1,-1,0,0,1,1,1 };
int dy[8] = { -1,0,1,-1,1,-1,0,1 };
void dfs(int x,int y) {//遍历的坐标
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];//nx为下一步的横坐标
int ny = y + dy[i];//ny为下一步的纵坐标
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (visited[nx][ny]) continue;
if (grid[nx][ny] == '.') continue;
visited[nx][ny] = 1;
dfs(nx, ny);
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++) {
scanf("%s", grid[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 'W') {
visited[i][j] = 1;
dfs(i, j);
res++;
}
}
}
cout << res;
return 0;
}
[USACO10OCT] Lake Counting S的更多相关文章
- [USACO10OCT]Lake Counting(DFS)
很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...
- 题解【洛谷P1596】[USACO10OCT]Lake Counting
题面 \(\text{Flood Fill}\) 模板题. \(\text{Flood Fill}\) 可以快速求出一个图中连通块的个数. 大概就是遍历每一个点,如果它没有被遍历过且是一个新连通块,那 ...
- 洛谷 P1596 [USACO10OCT]湖计数Lake Counting
题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...
- 洛谷——P1596 [USACO10OCT]湖计数Lake Counting
P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- bzoj1751 [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MB Submit: 168 Solved: 130 [ ...
- BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘
题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Description 农夫 ...
- 3385: [Usaco2004 Nov]Lake Counting 数池塘
3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 22 Solved: 21 ...
随机推荐
- IDEA 各个版本下载指引
1.IDEA 其它版本下载指引 网址: https://www.jetbrains.com.cn/idea/download/other.html 2.下载问题 下载哪个版本? win + R 打开命 ...
- 高并发报错too many clients already或无法创建线程
高并发报错 too many clients already 或无法创建线程 本文出处:https://www.modb.pro/db/432236 问题现象 高并发执行 SQL,报错"so ...
- 【课程汇总】Hello HarmonyOS系列课程,手把手带你零基础入门
HarmonyOS是面向未来.面向全场景的新一代智能终端操作系统,为不同设备的智能化.互联与协同提供了统一的语言,给人们带来简洁.流畅.连续.安全可靠的全场景交互体验. 初识HarmonyOS的开发者 ...
- Python3学习--正则表达式
import rer = re.match("Chen\d+","Chen123ronghua123")r = re.match(".+", ...
- B端架构升级之路
一.背景 随着B端业务快速发展,系统愈趋复杂.我们发起了B端架构升级专项,基于B端业务的特点,从研发规范建设.B端架构基建.系统架构升级和落地保障等多方面提升了B端的架构水平. 二.升级思路 架构是一 ...
- 重学c#系列——什么是性能[外篇性能篇一]
前言 简单写一下性能的简介. 正文 什么是性能,很多时候有一个问题,那就很多人喜欢说.这个服务有很多访问,我们需要这样设计. 这是一个无法验证的指标,访问次数是多少? 响应时间是多少. 我把这归纳为自 ...
- 抓包整理外篇——————https 抓包 [ 五]
前言 简单介绍一下https 抓包. 正文 可能有人一开始的时候发现抓包都抓不到,原因也很简单,那是因为https 需要解密. 那么为什么https 不解密呢? 那是因为证书信任问题. 下面先介绍,上 ...
- # AssertionError: The `num_classes` (3) in Shared2FCBBoxHead of MMDataParallel does not matches the length of `CLASSES` 80) in CocoDataset
我看很多人都遇到了这个问题,有很多解决了的.我就把这篇博文再完善一下,让大家对mmdetection使用得心应手. mmdetection训练自己的数据集时报错 ️ : # AssertionErro ...
- Greenplum Jdbc 调用 SETOF refcursor
最近公司需要用Greenplum,在调用 jdbc的时候遇到了一些问题.由于我们前提的业务都是使用 sqlserver,sqlserver的 procedure 在前端展示做数据源的时候才用的非常多, ...
- 蚂蚁一面:GC垃圾回收时,内存分配和回收策略有哪些?
文章首发于公众号:腐烂的橘子 蚂蚁面试主要为电话面试,期间也会要求使用编辑器手写算法题.作为一线互联网大厂,Java 基础知识是必备的,其中垃圾回收也是面试过程中的重中之重. Java 内存的自动管理 ...