题意翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为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×nn×n square. For each field (i,j)(i,j) belonging to the square(for i,j∈{1,⋯,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 SSit is possible to reach any other field in SS while moving only between adjacent fields and without leaving the set SS ),if s∈Ss∈S and the field s′∉Ss′∉S is adjacent to ss , then ws>ws′ws>ws′ (for a ridge) or ws

输入输出格式

输入格式:

In the first line of the standard input there is one integer nn ( 2 ≤ n ≤ 10002 ≤ 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∈{1,⋯,n}i∈{1,⋯,n} ) there are nn integers w(i,1),⋯,w(i,n)w(i,1),⋯,w(i,n) ( 0 ≤ wi ≤ 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
#include <cstdio>
#include <iostream>
using namespace std;
int n,hei[1010][1010],w;bool g[1010][1010];//w==1谷w==2峰
int mx[9]={0,-1,-1,-1,0,0,1,1,1},a=0,b=0;//a为山谷数 b为山峰数
int my[9]={0,-1,0,1,-1,1,-1,0,1};
void search(int posx,int posy)
{for(int i=1;i<=8;i++){
//搜索与当前点相邻的3~8个点int xx=posx+mx[i],yy=posy+my[i];if(!(xx>=1&&xx<=n&&yy>=1&&yy<=n))continue;if(hei[xx][yy]==hei[posx][posy]&&g[xx][yy]==0)g[xx][yy]=1,search(xx,yy);else if(hei[xx][yy]<hei[posx][posy]&&w==1)w=-1;else if(hei[xx][yy]>hei[posx][posy]&&w==2)w=-1;
//如果这个区域周围既有比它高的又有比它低的 则它什么也不是
//为了区分什么都不是的区域与没确定是是峰还是谷
// 前者为-1 后者为0else if(w==0){if(hei[xx][yy]<hei[posx][posy])w=2;if(hei[xx][yy]>hei[posx][posy])w=1;}}
//搜到与当前搜索高度不同的点 暂且标记当前搜索区域是峰还是谷
}
int main()
{scanf("%d",&n);    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&hei[i][j]);int x=hei[1][1];    w=1;for(int i=2;i<=n;i++)if(hei[1][i]!=x)w=0;if(w==1)for(int i=2;i<=n;i++)for(int j=1;j<=n;j++)if(hei[i][j]!=x)w=0;if(w==1){cout<<"1 1";return 0;
//特判 若所有高度相同 则山峰数=1 山谷数=1}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(g[i][j]==0){
//找到一个没搜过的点开始搜索w=0;g[i][j]=1;search(i,j);if(w==1)a++;else if(w==2)b++;}    }    }    printf("%d %d",b,a);
//输出答案return 0;
}

洛谷P3456 [POI2007]GRZ-Ridges and Valleys相关推荐

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

    [POI2007]TET-Tetris Attack 题目描述 一种名为 Tetris Attack 的猜谜游戏风靡 Byteotia.游戏本身非常复杂,因此我们只介绍它的简化规则: 玩家拥有一个有 ...

  2. 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯反演)

    题意: 给定a,b,d求gcd(x,y)=d的对数(1<=x<=a,1<=y<=b) 思路:按照套路来先设f(n)为gcd(x,y)=n的对数,g(n)表示为 n | gcd( ...

  3. 洛谷P2522 [HAOI2011]Problem b(莫比乌斯反演)

    传送门 我们考虑容斥,设$ans(a,b)=\sum_{i=1}^a\sum_{j=1}^b[gcd(a,b)==k]$,这个东西可以和这一题一样去算洛谷P3455 [POI2007]ZAP-Quer ...

  4. P3456 [POI2007]GRZ-Ridges and Valleys

    题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是相邻的格子.(所以与(i,j)相邻的格子有(i ...

  5. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  6. 洛谷 P1142 轰炸

    洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...

  7. 洛谷 P1387 最大正方形

    P1387 最大正方形 题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=10 ...

  8. 洛谷P2763 试题库问题

    题目:https://www.luogu.org/problemnew/show/P2763 题目描述 «问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性. ...

  9. 动态规划——洛谷_P1057传球游戏

    题目: 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏.游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球, ...

  10. 洛谷P1417 烹调方案

    洛谷P1417 烹调方案 如果是一般的01背包的话 选的先后是没关系的 但是这题选的先后是有关系的,因为他的价值是随着时间而变化的, 而你的01背包是做不到先选2再选1的 那么我们就跟国王游戏一样 用 ...

最新文章

  1. olap 多维分析_如何通过依赖T-SQL从OLAP多维数据集有效地提取数据
  2. python识别验证码ocr_Python3使用tesserocr识别字母数字验证码
  3. 哈尔滨冰景:映衬时代主题
  4. 微信消息推送之过长的文本消息拆分踩坑
  5. ecshop百度收录插件,ECSHOP一键百度推送收录,ECSHOP一键百度收录
  6. ppt画图画不下——调整ppt页面的大小
  7. typora里面如何快捷改变字体颜色?
  8. macmini作为远程服务器,我在用我的 Mac mini 做什么
  9. JavaEE 微信境外支付
  10. 如何让大学的青春少些遗憾!
  11. 第一章 JavaWEB专题之Http基础协议解析
  12. Apache http Server与Tomcat整合 2
  13. 什么是soft wrap,什么是IDEA的soft wrap,如何设置IDEA默认所有类型的文件都自动换行(如何设置用IDEA打开markdown文件不自动换行)
  14. 堆和栈访问效率哪个更高
  15. java随机生成姓名、电话、邮箱、时间
  16. java 字节码操作图和JAVAssist库图
  17. 搞搞吧的模式方式值得我们学习
  18. 【论文笔记】SCOAT-Net: A novel network for segmenting COVID-19 lung opacification from CT images
  19. 解读湖北省8月双防政策,2022下半场化工厂人员定位大洗牌
  20. [Jsoi2013]快乐的jyy

热门文章

  1. 微积分学习笔记(2)--修改更新中
  2. 网页POST之文件上传专项视频教程
  3. 计算机excel怎么删除重复项,excel怎么删除重复项
  4. bootstrap+javascript制作体重标准计算器
  5. 网络准入控制 — 保护网络安全
  6. realize与recognize辨析
  7. C#编写中英文虚拟键盘
  8. python 天天向上求几天数_Python十题(第2课)
  9. ThinkPHP教程
  10. Creo 9.0 基准特征:基准轴