洛谷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×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 后者为0
        else 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;
}

猜你喜欢

转载自blog.csdn.net/zhaohaibo_/article/details/80456020