题意翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为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\times nn×n square. For each field (i,j)(i,j)(i,j) belonging to the square(for i,j∈{1,⋯,n}i,j\in \{1,\cdots,n\}i,j∈{1,⋯,n} ), its height w(i,j)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)(i,j) is adjacent to the fields (i−1,j−1)(i-1,j-1)(i−1,j−1) , (i−1,j)(i-1,j)(i−1,j) , (i−1,j+1)(i-1,j+1)(i−1,j+1) , (i,j−1)(i,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−1) , (i+1,j)(i+1,j)(i+1,j) , (i+1,j+1)(i+1,j+1)(i+1,j+1) , provided that these fields are on the map).

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

all the fields in SSS have the same height,the set SSS forms a connected part of the map (i.e. from any field in SSS it is possible to reach any other field in SSS while moving only between adjacent fields and without leaving the set SSS ),if s∈Ss\in Ss∈S and the field s′∉Ss'\notin Ss′∉S is adjacent to sss , then ws>ws′w_s>w_{s'}ws​>ws′​ (for a ridge) or ws<ws′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 nnn ( 2≤n≤1 0002\le n\le 1\ 0002≤n≤1 000 )denoting the size of the map. Ineach of the following nnn lines there is the description of the successive row of the map. In (i+1)(i+1)(i+1) 'th line(for i∈{1,⋯,n}i\in \{1,\cdots,n\}i∈{1,⋯,n} ) there are nnn integers w(i,1),⋯,w(i,n)w_{(i,1)},\cdots,w_{(i,n)}w(i,1)​,⋯,w(i,n)​ ( 0≤wi≤1 000 000 0000\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 iii '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

Solution:

  本题比较简单,直接$floodfill$(水漫金山),开始写了个广搜,结果超时$2$个点,后面听信神佬的话改了个深搜填充,还是$T2$个点。

  结果果然是$memset$的原因,于是骚操作不用将标记清$0$,果然$AC$。(话说快了$2500ms$,真$TM$神奇~~)

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=,dx[]={,-,,,,-,,-},dy[]={,,,-,,-,-,};
ll n,ans1,ans2,cnt,f,p,ct[N][N];
bool vis[N][N];
struct node{
int x,y,w;
}a[N][N];
il ll gi(){
ll a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
}
il void dfs(int x,int y){
vis[x][y]=,ct[x][y]=a[x][y].w;
For(i,,){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=&&xx<=n&&yy>=&&yy<=n){
if(ct[xx][yy]==a[x][y].w)continue;
if(a[xx][yy].w==a[x][y].w)dfs(xx,yy);
else {
if(!f&&!p&&a[xx][yy].w>a[x][y].w)p=;
else if(!f&&!p&&a[xx][yy].w<a[x][y].w)p=;
else if(p==&&!f&&a[xx][yy].w<a[x][y].w)f=;
else if(p==&&!f&&a[xx][yy].w>a[x][y].w)f=;
else continue;
}
}
}
}
int main(){
n=gi();
For(i,,n) For(j,,n) a[i][j].w=gi(),a[i][j].x=i,a[i][j].y=j;
For(i,,n) For(j,,n)
if(!vis[i][j]){
p=f=;
dfs(i,j);
if(f)continue;
if(p==)ans2++;
else ans1++;
}
if(ans1==&&!ans2||ans2==&&!ans1)ans1=ans2=;
cout<<ans1<<' '<<ans2;
return ;
}

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

  1. 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys

    P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...

  2. P3456 [POI2007]GRZ-Ridges and Valleys(bfs)

    P3456 [POI2007]GRZ-Ridges and Valleys 八个方向都跑一遍bfs,顺便判断一下是山峰还是山谷,或者是山坡(俩都不是) (实在不知道要说啥了qwq) #include& ...

  3. BZOJ1102 [POI2007]GRZ山峰和山谷 [BFS]

    题目传送门 GRZ山峰和山谷 Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要 ...

  4. 山峰和山谷 Ridges and Valleys

    题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周 ...

  5. bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm

    P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 按海拔是否相同分块 每次bfs海拔相 ...

  6. 「CSP-S模拟赛」2019第三场

    目录 T1 「POI2007」山峰和山谷 Ridges and Valleys 题目 考场思路(几近正解) 正解 T2 「JOI 2013 Final」 现代豪宅 题目 考场思路(正解) T3 「SC ...

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

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

  8. bzoj1102: [POI2007]山峰和山谷Grz

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. [POI2007]山峰和山谷Grz

    Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...

随机推荐

  1. git 常用命令及仓库创建

    一.常用命令 1.添加到本地仓库缓存 git add . 2.查看本地仓库状态 git status 3.提交到本地仓库 git commit -am 'project init' 4.连接线上分支 ...

  2. 谈谈对bug的一点想法,说说做好日志记录的重要性

    说起程序猿,总绕不开的一个话题就是bug,估计每个程序猿听到某某测试跑过来一脸淫笑的告诉你这个功能有个bug的时候,总有种恨不得掐死他的想法.其实程序猿跟bug的关系,感觉有点像父亲和儿子的关系,自己 ...

  3. 微信小程序本地缓存

  4. 利用python进行坐标提取以及筛选(文件操作的小应用)

    由于目前暂时还未学习到python关于数据处理的模块方面的知识,且刚好最近朋友发来一份坐标数据文件(txt格式),让我帮他对其进行筛选, 因此利用了最近刚学过的python文件处理操作以及以前所学的基 ...

  5. 009---Django的模型层(1)

    ORM 全称:对象--关系--映射 数据表----类 数据行----对象 字 段----属性优势:专注于后端逻辑,不用写复杂的sql语句劣势:会忘掉sql,牺牲了效率 Django连接mysql数据库 ...

  6. PTA 7-12(图) 社交网络图中结点的“重要性”计算 最短路

    7-12(图) 社交网络图中结点的“重要性”计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的 ...

  7. python-13常用内建模块

    1-datetime #1-获取当前日期和时间 from datetime import datetime now = datetime.now() #当前时间 print(now) #2015-05 ...

  8. C#学习你需要知道的---(For和Foreach)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52577283 作者:car ...

  9. Django 2.0官方文档中文 总索引

    Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本:  ...

  10. 做出一个SwitchButton的效果,并详细学习一下onDraw(Canvas canvas)方法的使用

    代码的灵感和原理主要来自于android自定义开关控件-SlideSwitch http://blog.csdn.net/singwhatiwanna/article/details/9254309这 ...