Bzoj1102: [poi2007] Mountain and valley grz

Source: Internet
Author: User
1102: [poi2007] Mountain and valley grztime limit: 10 sec memory limit: 162 MB
Submit: 436 solved: 227
[Submit] [Status] Description

FGD is especially fond of mountain climbing. He is studying mountain peaks and valleys. In order for him to have an arrangement on his journey, he wanted to know the number of mountains and valleys.
Given a map, for the area that FGD wants to travel, the map is divided into N * n grids, and the height of each grid (I, j) is W (I, j) given.
If two grids have common vertices, they are adjacent grids. (So the grids adjacent to (I, j) are (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 )).
We define a grid set S as a mountain (Valley) When and only when:
All the grids of 1. s have the same height.
2. All grids of S are connected
3. If s belongs to S, and s Adjacent to S does not belong to S. There are ws> ws '(mountain) or ws <ws' (Valley ).
Your task is to find the number of peaks and valleys for a given map. If all grids have the same height, the whole map is a mountain and a valley.

Input

The first line contains a positive integer N, indicating the map size (1 <= n <= 1000 ). The next N * n matrix represents the height of each grid on the map. (0 <= W <= 1000000000)

Output

It should contain two numbers, indicating the number of mountain and valley, respectively.

Sample input Example 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
Input Example 2
5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7 sample output sample 1
2 1
Output Example 2
3 3
Hint

Source

Question:

I don't need to write any code...

Post a personal

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 using namespace std; 6    7 const int px[8]={-1,-1,0,1,1,1,0,-1}; 8 const int py[8]={0,1,1,1,0,-1,-1,-1}; 9 int map[1001][1001],x[1000001],y[1000001],n,valleys,ridges;10 bool vis[1001][1001];11   12 bool check(int pos,char sym){13     for(int k=1;k<=pos;k++)14         for(int i=0;i<8;i++){15             int xx=x[k]+px[i],yy=y[k]+py[i];16             if(!(xx>0&&xx<=n&&yy>0&&yy<=n)) continue;17             if((sym==‘v‘&&map[xx][yy]<map[x[k]][y[k]])||(sym==‘r‘&&map[xx][yy]>map[x[k]][y[k]])) return false;18         }19     return true;20 }         21   22 void bfs(int posx,int posy){23     int head=1,tail=0,xx,yy;24     x[++tail]=posx,y[tail]=posy;25     while(head<=tail){26         for(int i=0;i<8;i++){27             xx=x[head]+px[i],yy=y[head]+py[i];28             if(!(xx>0&&xx<=n&&yy>0&&yy<=n&&map[xx][yy]==map[posx][posy]&&!vis[xx][yy])) continue;29             vis[xx][yy]=true;30             x[++tail]=xx,y[tail]=yy;31         }32         head++;33     }34     if(check(tail,‘v‘)) valleys++;35     if(check(tail,‘r‘)) ridges++;36 }37   38 int main(){39     scanf("%d",&n);40     for(int i=1;i<=n;i++)41         for(int j=1;j<=n;j++) scanf("%d",&map[i][j]);42     memset(vis,false,sizeof(vis));43     for(int i=1;i<=n;i++)44         for(int j=1;j<=n;j++)45             if(!vis[i][j]) bfs(i,j);46     printf("%d %d\n",ridges,valleys);47     return 0;48 }
View code

 

Bzoj1102: [poi2007] Mountain and valley grz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.