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\ ...
随机推荐
- 在VSCode中使用码云
在VSCode中使用码云 一.SSH公钥 使用SSH公钥可以让你在你的电脑和码云通讯的时候使用安全连接(Git的Remote要使用SSH地址) 链接 https://gitee.com/profile ...
- C++使用GDI+实现图片格式转换
主要是我在设置壁纸时遇到的个小问题,因为设置壁纸只能是bmp格式的图片,不可能我喜欢的壁纸就都是bmp格式的,就想怎么转换一下图片的格式,于是就在百度搜怎么弄,搜到了可行方法,却没有实现代码,有些看起 ...
- BZOJ1854: [Scoi2010]游戏(二分图匹配)
题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备 ...
- Fiddler(一)
Fiddler:学习scrapy,不只是满足于网页上爬去信息的成功乐趣,现在开始接触爬去手机信息了,不好解决,知道过程不会轻松,但自己想去尝试.QAQ 写这篇博客是基于以下的几位大神学习笔记,我只是做 ...
- SqlServer2008/2005数据库日志收缩
1.SQL2008数据库USE [master]GOALTER DATABASE 数据库名称 SET RECOVERY SIMPLE WITH NO_WAITALTER DATABASE 数据库名称 ...
- 创建控制器view的几种方式
1. 根据storyboard的描述创建 2. 通过xib的描述创建 3. 通过代码创建控制器的view self.window = [[UIWindow alloc] initWithFrame:[ ...
- 第三章 文件 I/O
3.1 引言 先说明可用的文件 I/O 函数:open.read.write.close,然后说明不同缓冲区长度对read和write函数的影响. 本章所说的函数经常被称为不带缓冲的 I/O (unb ...
- zookeeper: web ui工具的安装
zookeeper官方没有提供web用户界面,这使很多人在使用zookeeper(动物管理员)同时,并不是很容易的理解zookeeper的数据结构,还好淘宝有位大神用Nodejs写了一个web的ui工 ...
- cocos2d-x 3.0的入门程序:helloworld
看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...
- CC3200在AP模式的TCP sock作为客户端连接时返回SL_ECONNREFUSED(-111) Connection refused
1. CC3200处于AP模式(电脑无线连接CC3200的WIFI信号),开启一个TCP socket,这个socket作为TCP客户端去连接TCP服务器端 struct sockaddr_in ad ...