题意翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的。若两个格子有公共顶点,那么他们就是相邻的格子。(所以与(i,j)相邻的格子有(i-1, j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1))。我们定义一个格子的集合S为山峰(山谷)当且仅当:

1.S的所有格子都有相同的高度。

2.S的所有格子都联通3.对于s属于S,与s相邻的s’不属于S。都有ws > ws’(山峰),或者ws < ws’(山谷)。

你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。

输入 第一行包含一个正整数n,表示地图的大小(1<=n<=1000)。接下来一个n*n的矩阵,表示地图上每个格子的高度。(0<=w<=1000000000)

输出 应包含两个数,分别表示山峰和山谷的数量。

感谢@Blizzard 提供的翻译

题目描述

Byteasar loves trekking in the hills. During the hikes he explores all the ridges and valleys in vicinity.

Therefore, in order to plan the journey and know how long it will last, he must know the number of ridgesand valleys in the area he is going to visit. And you are to help Byteasar.

Byteasar has provided you with a map of the area of his very next expedition. The map is in the shape ofa n\times nn×nsquare. For each field (i,j)(i,j) belonging to the square(for i,j\in \{1,\cdots,n\}i,j∈{1,⋯,n} ), its height w_{(i,j)}w(i,j)​ is given.

We say two fields are adjacent if they have a common side or a common vertex (i.e. the field (i,j)(i,j) is adjacent to the fields (i-1,j-1)(i−1,j−1) , (i-1,j)(i−1,j) , (i-1,j+1)(i−1,j+1) , (i,j-1)(i,j−1) , (i,j+1)(i,j+1) , (i+1,j-1)(i+1,j−1) , (i+1,j)(i+1,j) , (i+1,j+1)(i+1,j+1) , provided that these fields are on the map).

We say a set of fields SS forms a ridge (valley) if:

all the fields in SS have the same height,the set SS forms a connected part of the map (i.e. from any field in SS it is possible to reach any other field in SS while moving only between adjacent fields and without leaving the set SS ),if s\in Ss∈S and the field s'\notin Ss′∉S is adjacent to ss , then w_s>w_{s'}ws​>ws′​ (for a ridge) or w_s<w_{s'}ws​<ws′​ (for a valley).

In particular, if all the fields on the map have the same height, they form both a ridge and a valley.

Your task is to determine the number of ridges and valleys for the landscape described by the map.

TaskWrite a programme that:

reads from the standard input the description of the map, determines the number of ridges and valleys for the landscape described by this map, writes out the outcome to the standard output.

给定一张地势图,求山峰和山谷的数量

输入输出格式

输入格式:

In the first line of the standard input there is one integer nn ( 2\le n\le 1\ 0002≤n≤1 000 )denoting the size of the map. Ineach of the following nn lines there is the description of the successive row of the map. In (i+1)(i+1) 'th line(for i\in \{1,\cdots,n\}i∈{1,⋯,n} ) there are nn integers w_{(i,1)},\cdots,w_{(i,n)}w(i,1)​,⋯,w(i,n)​ ( 0\le w_i\le 1\ 000\ 000\ 0000≤wi​≤1 000 000 000 ), separated by single spaces. Thesedenote the heights of the successive fields of the ii 'th row of the map.

输出格式:

The first and only line of the standard output should contain two integers separated by a single space -thenumber of ridges followed by the number of valleys for the landscape described by the map.

输入输出样例

输入样例#1: 复制

5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8
输出样例#1: 复制

2 1
思路:水题,没看见上面两句话,炸了两次,gg;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,ans,bns,flag,hg;
int map[][],vis[][];
void dfs(int x,int y,int col){
if(x<||x>n||y<||y>n) return ;
if(map[x][y]!=col&&map[x][y]>col&&flag==) flag++;
else if(map[x][y]!=col&&map[x][y]<col&&flag==) flag--;
else if(map[x][y]!=col&&map[x][y]>col&&flag<) hg=;
else if(map[x][y]!=col&&map[x][y]<col&&flag>) hg=;
if(map[x][y]!=col||vis[x][y]) return ;
vis[x][y]=;
dfs(x+,y,col);dfs(x-,y,col);dfs(x+,y+,col);dfs(x-,y-,col);
dfs(x,y+,col);dfs(x,y-,col);dfs(x+,y-,col);dfs(x-,y+,col);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&map[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(!vis[i][j]){
flag=;hg=;
dfs(i,j,map[i][j]);
if(hg==&&flag<=) ans++;
if(hg==&&flag>=) bns++;
}
cout<<ans<<" "<<bns;
}
 

洛谷 P3456 [POI2007]GRZ-Ridges and Valleys的更多相关文章

  1. [洛谷P3460] [POI2007]TET-Tetris Attack

    洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...

  2. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

  3. 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)

    洛谷题目传送门 正解是树状数组维护dfn序上的前缀和,这样的思路真是又玄学又令我惊叹( 我太弱啦,根本想不到)Orz各路Dalao 今天考了这道题,数据范围还比洛谷的小,只有\(10^5\)(害我复制 ...

  4. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

  5. 洛谷 P3462 [POI2007]ODW-Weights

    题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...

  6. 题解【洛谷P3456】[POI2007]GRZ-Ridges and Valleys

    题面 考虑 \(\text{Flood Fill}\). 每次在 \(\text{BFS}\) 扩展的过程中增加几个判断条件,记录山峰和山谷的个数即可. #include <bits/stdc+ ...

  7. 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)

    题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...

  8. 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

    [POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...

  9. 【刷题】洛谷 P3455 [POI2007]ZAP-Queries

    题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...

随机推荐

  1. css3中的变换、动画和过渡

    变换:分为2d变换和3d变换,但一次只能用一个变换属性,多个的话最后一个会覆盖前面所有最终被浏览器实现,变换可以成为过渡和动画的一个待变参数(类似width,opacity等). 过渡:是动画的初始模 ...

  2. Spring---AOP注解开发&jdbc模板&Spring事务管理

    一.AOP注解开发 此处需要回忆一遍AOP的概念.简单的来说,AOP就是利用动态代理技术,做到不触动源代码但却扩展了功能.那么就需要一个被扩展的对象和一个“新的功能”,例如说给某类的saveUser方 ...

  3. Thinkphp删除缓存

    控制器代码   public function delcache(){ //当找到有Runtime的文件夹时,进入if if(is_dir(RUNTIME_PATH)){ delDir(RUNTIME ...

  4. Android动态权限申请

    Android系统中,目前Dangerous级别的权限都需要动态申请.步骤如下: 1.AndroidManfiest.xml中申明需要的动态权限 <?xml version="1.0& ...

  5. Laravel 的 API 认证系统 Passport 三部曲(二、passport的具体使用)

    GQ1994 关注 2018.04.20 09:31 字数 1152 阅读 1316评论 0喜欢 1 参考链接 Laravel 的 API 认证系统 Passport 三部曲(一.passport安装 ...

  6. java.math.BigDecimal类multiply的使用

    java.math.BigInteger.multiply(BigInteger val) 返回一个BigInteger,其值是 (this * val).声明 以下是java.math.BigInt ...

  7. day21-3 类的组合

    目录 类的组合 组合的应用 类的组合 组合就是一个类的对象具备某一个属性,该属性的值是指向另外一个类的对象 组合的好处:解决类与类之间代码冗余的问题 组合的应用 需求:假如我们需要给学生增添课程属性, ...

  8. VINS-Mono论文笔记(未完)

    这是整篇论文的架构,下面针对每一部分进行自己的详细理解.(数学公式的问题没在博客里面解决,都是论文中的截图,尽可能美观==) 一.测量预处理部分(MEASUREMENT PREPROCESSING) ...

  9. 奇异值分解 SVD 的数学解释

    奇异值分解(Singular Value Decomposition,SVD)是一种矩阵分解(Matrix Decomposition)的方法.除此之外,矩阵分解还有很多方法,例如特征分解(Eigen ...

  10. oracle学习链接

    http://www.cnblogs.com/huyong/category/646939.html