题意翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为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$神奇~~)

代码:

#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 ;
}

最新文章

  1. entityframework学习笔记--009-使用原生sql语句操作数据
  2. java线程池(newSingleThreadExecutor())小应用
  3. YUM源
  4. Office 365 系列一 ------- 如何单个安装Office 客户端和Skype for business
  5. 操作系统开发系列—12.d.扩充内核 ●
  6. CodeForces #369 C. Coloring Trees DP
  7. chrome断点续传功能
  8. typedef 和 const
  9. 025-ViewData、ViewBag与TempData概述
  10. C++质因式分解
  11. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9
  12. 【转载】java数据库操作
  13. Jenkins 七: 部署到Tomcat
  14. C#this的五种用法
  15. Keil C51程序设计中几种精确延时方法
  16. spserver 开源服务器框架研究与分析
  17. JAVA异常处理、常用类、反射、集合
  18. 2016大连网络赛 Sparse Graph
  19. 2018/1/21 Netty通过解码处理器和编码处理器来发送接收POJO,Zookeeper深入学习
  20. 三位数流水码的生成(000&#183;&#183;&#183;&#183;&#183;009&#183;&#183;00A&#183;&#183;&#183;&#183;00Z&#183;&#183;&#183;&#183;ZZZ)

热门文章

  1. Linux 性能检查常用命令
  2. map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
  3. github上更新fork项目
  4. Nginx+php+mysql+wordpress搭建自己的博客站点
  5. Linux Centos 通过虚拟用户访问FTP的配置
  6. 微信小程序开发入门学习(1):石头剪刀布小游戏
  7. JS下载文件常用的方式
  8. ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)
  9. 内置函数系列之 sorted排序
  10. PHP 防止 E-mail 注入( PHP 过滤器)