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×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.
输入输出样例
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
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的更多相关文章
- 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys
P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...
- P3456 [POI2007]GRZ-Ridges and Valleys(bfs)
P3456 [POI2007]GRZ-Ridges and Valleys 八个方向都跑一遍bfs,顺便判断一下是山峰还是山谷,或者是山坡(俩都不是) (实在不知道要说啥了qwq) #include& ...
- BZOJ1102 [POI2007]GRZ山峰和山谷 [BFS]
题目传送门 GRZ山峰和山谷 Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要 ...
- 山峰和山谷 Ridges and Valleys
题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周 ...
- bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 按海拔是否相同分块 每次bfs海拔相 ...
- 「CSP-S模拟赛」2019第三场
目录 T1 「POI2007」山峰和山谷 Ridges and Valleys 题目 考场思路(几近正解) 正解 T2 「JOI 2013 Final」 现代豪宅 题目 考场思路(正解) T3 「SC ...
- 题解【洛谷P3456】[POI2007]GRZ-Ridges and Valleys
题面 考虑 \(\text{Flood Fill}\). 每次在 \(\text{BFS}\) 扩展的过程中增加几个判断条件,记录山峰和山谷的个数即可. #include <bits/stdc+ ...
- bzoj1102: [POI2007]山峰和山谷Grz
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [POI2007]山峰和山谷Grz
Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...
随机推荐
- 阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件
项目中用到了oss上传,用的plupload,奈何样式上不敢恭维,特别是放在移动端上使用.于是自己把它移植到了jq weui的上传图片组件上. 更改:选择照片后确认即及时上传至oss服务器,不限制上传 ...
- react的 react-router使用
官方API:https://reacttraining.com/react-router/web/api/BrowserRouter; React Router 安装命令如下. 使用时,路由器Rout ...
- 关于var和ES6中的let,const的理解
var的作用就不多说了,下面说说var的缺点: 1.var可以重复声明 var a = 1; var a = 5; console.log(a); //5 不会报错 在像这些这些严谨的语言来说,一般是 ...
- BeanFactory和IOC控制反转
之前在看spring,看IOC实在是云里雾里,包括看AOP也是云里雾里的,后来重新学习Java Web,做了一个简单的web项目,再之后看了崔希凡老师的视频,Day27和Day28两天的内容,真的很有 ...
- 记 判断手机号运营商function
/* 移动:134.135.136.137.138.139.150.151.157(TD).158.159.187.188 联通:130.131.132.152.155.156.185.186 电信: ...
- 数据分析处理库Pandas——对象操作
Series结构 索引 修改 旧数据赋值给新数据,旧数据不变. 对某一数值进行修改,可以选择保留修改前或修改后的数值. 替换索引 修改某一个索引 添加 在数据1后添加数据2,数据1不改变. 添加一个数 ...
- E - Nature Reserve CodeForces - 1059D
传送门 There is a forest that we model as a plane and live nn rare animals. Animal number iihas its lai ...
- BZOJ 1441: Min(裴蜀定理)
BZOJ 1441:Min Description 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 Input 第一行给出数 ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- python-1基础总结
输入 >>> name = input() 1--如果字符串里面有很多字符都需要转义,就需要加很多\,为了简化,Python还允许用r''表示''内部的字符串默认不转义,可以自己试 ...