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.

输入输出样例

输入样例#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$神奇~~)

代码:

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 #define ll long long
 4 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 5 using namespace std;
 6 const int N=1005,dx[8]={1,-1,0,0,1,-1,1,-1},dy[8]={0,0,1,-1,1,-1,-1,1};
 7 ll n,ans1,ans2,cnt,f,p,ct[N][N];
 8 bool vis[N][N];
 9 struct node{
10     int x,y,w;
11 }a[N][N];
12 il ll gi(){
13     ll a=0;char x=getchar();
14     while(x<‘0‘||x>‘9‘)x=getchar();
15     while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar();
16     return a;
17 }
18 il void dfs(int x,int y){
19     vis[x][y]=1,ct[x][y]=a[x][y].w;
20     For(i,0,7){
21         int xx=x+dx[i],yy=y+dy[i];
22         if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
23             if(ct[xx][yy]==a[x][y].w)continue;
24             if(a[xx][yy].w==a[x][y].w)dfs(xx,yy);
25             else {
26                 if(!f&&!p&&a[xx][yy].w>a[x][y].w)p=1;
27                 else if(!f&&!p&&a[xx][yy].w<a[x][y].w)p=2;
28                 else if(p==1&&!f&&a[xx][yy].w<a[x][y].w)f=1;
29                 else if(p==2&&!f&&a[xx][yy].w>a[x][y].w)f=1;
30                 else continue;
31             }
32         }
33     }
34 }
35 int main(){
36     n=gi();
37     For(i,1,n) For(j,1,n) a[i][j].w=gi(),a[i][j].x=i,a[i][j].y=j;
38     For(i,1,n) For(j,1,n)
39     if(!vis[i][j]){
40         p=f=0;
41         dfs(i,j);
42         if(f)continue;
43         if(p==1)ans2++;
44         else ans1++;
45     }
46     if(ans1==1&&!ans2||ans2==1&&!ans1)ans1=ans2=1;
47     cout<<ans1<<‘ ‘<<ans2;
48     return 0;
49 }

原文地址:https://www.cnblogs.com/five20/p/9038487.html

时间: 2024-07-31 10:30:46

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

山峰和山谷 Ridges and Valleys

题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周围的点都看一遍和这个点高度相同的就入队,把高度相同的点都打上浏览的标记.看的过程中,既有比它小的,也有比它大的,那么这种高度的点就不是我们要统计的答案,只有比它小的,或只有比它大的,那么就是我们要统计的.可以设置两个标记,标记看的过程中有没有比它小的,比它大的. 然后就是只有一个高度的时候,因为两个

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

P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 每次bfs海拔相同的块,根据与周围的块的大小关系判断是否是山丘. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<cctype> 6 #define re

BZOJ1102: [POI2007]山峰和山谷Grz

1102: [POI2007]山峰和山谷Grz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 436  Solved: 227[Submit][Status] Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量. 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的. 若两个格子有公共顶点,那么他们就

【BZOJ 1102】 [POI2007]山峰和山谷Grz

1102: [POI2007]山峰和山谷Grz Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 495  Solved: 263 [Submit][Status][Discuss] Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量. 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的. 若两个格子

BZOJ 1102 POI2007 山峰和山谷Grz Floodfill

题目大意:给定一张地势图,求山峰和山谷的数量 直接Floodfill--注意DFS会爆栈,用BFS才能过 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1010 using namespace std; int n,ans1,ans2,a[M][M]; bool flag,v[M][M]; /* void Floodfill(in

[POI2007]山峰和山谷Grz

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

【BZOJ 1103】 [POI2007]大都市meg

1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1292  Solved: 660 [Submit][Status] Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n的n个小村庄,某些村庄之间有一些双向的土路.从每个村庄都恰好有一条路径到达村庄1(即比特

BZOJ 1098[POI2007]办公楼

题面: 1098: [POI2007]办公楼biu Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1371  Solved: 641[Submit][Status][Discuss] Description FGD开办了一家电话公司.他雇用了N个职员,给了每个职员一部手机.每个职员的手机里都存储有一些同事的电话号码.由于FGD的公司规模不断扩大,旧的办公楼已经显得十分狭窄,FGD决定将公司迁至一些新的办公楼.FGD希望职员被安置在尽量多的办公楼当

bzoj1103[POI2007]大都市meg

bzoj1103[POI2007]大都市meg 题意: 一个n点树,根节点为1,初始时全部边为土路,共n-m+1次操作,每次可将一条边改为公路或求根节点到某个节点要几个多少土路.n,m≤250000 题解: 先求出DFS序,进入节点在时间点的权值为1,离开节点在时间点的权值为-1,如果把公路转成土路就将这条边的左端点的进入时间权值和离开时间权值都置为0,如果询问则输出节点进入时间前缀和.求前缀和及修改的操作可以用树状数组维护. 代码: 1 #include <cstdio> 2 #includ