洛谷 P3456 [POI2007]GRZ-Ridges and Valleys
题意翻译
给定一个地图,为小朋友想要旅行的区域,地图被分为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.
输入输出样例
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
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的更多相关文章
- [洛谷P3460] [POI2007]TET-Tetris Attack
洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...
- [洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)
洛谷题目传送门 正解是树状数组维护dfn序上的前缀和,这样的思路真是又玄学又令我惊叹( 我太弱啦,根本想不到)Orz各路Dalao 今天考了这道题,数据范围还比洛谷的小,只有\(10^5\)(害我复制 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- 洛谷 P3462 [POI2007]ODW-Weights
题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...
- 题解【洛谷P3456】[POI2007]GRZ-Ridges and Valleys
题面 考虑 \(\text{Flood Fill}\). 每次在 \(\text{BFS}\) 扩展的过程中增加几个判断条件,记录山峰和山谷的个数即可. #include <bits/stdc+ ...
- 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]
[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...
- 【刷题】洛谷 P3455 [POI2007]ZAP-Queries
题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...
随机推荐
- JS中的逻辑运算符&&、||,位运算符|,&
1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...
- windows server 2008 r2 IIS7下网站配置 只允许指定的IP地址访问
步骤一.找到ip地址和域限制 步骤二.添加全部拒绝 步骤三.添加允许访问的ip地址(局域网填写局域网ip,公网填写公网ip) 步骤四:如果想要拒绝某些ip访问,直接在规则中添加拒绝条目就可以
- java实现的单点登录
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- React和webpack解决 waiting for roots to load...to reload the inspector
使用chrome调试工具,react-devtools总是显示 "waiting for roots to load...to reload the inspector" and ...
- 实训day01 python基础
一.编程语言 编程语言:可以被计算机所识别的表达方式. 编程:程序员通过编程语言将自己的想法编写出来,产生的结果就是包含字符的文件. 其中,只有程序在运行时,其中的字符才有特定的语法意义. 二.计算机 ...
- Android图像处理之BitMap(2)
Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...
- iOS视频通话方案
现在iPhone4平台上实时音视频对话已取得初步成果.其间查阅了很多资料,感谢这些信息的提供者.继往开来,我写下此文.我只列出要点,具体编码以及平台移植各位自己去努力吧.照着下面的步骤,您一定能做出来 ...
- react-native 框架升级 安卓第三方插件报错 Android resource linking failed
亲自经历react-native从0.55升级到0.58的过程,有点坎坷,ios出现的问题还算不多,但是android这里,随着gradle和buildTool的使用升级,导致第三方插件出现各种问题, ...
- Git 教程 -- 第一天
什么是Git? Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. 为什么使用Git? 众所周知,版本控制系统分为集中式版本控制系统(SVN.CVS等)与分布式版 ...
- (thinkPHP)PHP常用函数大全
usleep() 函数延迟代码执行若干微秒.unpack() 函数从二进制字符串对数据进行解包.uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.time_sleep_until() ...