题意翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为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=1005,dx[8]={1,-1,0,0,1,-1,1,-1},dy[8]={0,0,1,-1,1,-1,-1,1};
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=0;char x=getchar();while(x<'0'||x>'9')x=getchar();while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar();return a;
}
il void dfs(int x,int y){vis[x][y]=1,ct[x][y]=a[x][y].w;For(i,0,7){int xx=x+dx[i],yy=y+dy[i];if(xx>=1&&xx<=n&&yy>=1&&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=1;else if(!f&&!p&&a[xx][yy].w<a[x][y].w)p=2;else if(p==1&&!f&&a[xx][yy].w<a[x][y].w)f=1;else if(p==2&&!f&&a[xx][yy].w>a[x][y].w)f=1;else continue;}}}
}
int main(){n=gi();For(i,1,n) For(j,1,n) a[i][j].w=gi(),a[i][j].x=i,a[i][j].y=j;For(i,1,n) For(j,1,n)if(!vis[i][j]){p=f=0;dfs(i,j);if(f)continue;if(p==1)ans2++;else ans1++;}if(ans1==1&&!ans2||ans2==1&&!ans1)ans1=ans2=1;cout<<ans1<<' '<<ans2;return 0;
}

转载于:https://www.cnblogs.com/five20/p/9038487.html

P3456 [POI2007]GRZ-Ridges and Valleys相关推荐

  1. 山峰和山谷 Ridges and Valleys(bfs)

    山峰和山谷 Ridges and Valleys 题意 水平竖直和斜方向是一个点数值的比较对象,一共八个.相同的的高度可以连接在一起,形成山谷.山峰或者啥都不是.如果对于一个连接在一起的一块地方,其任 ...

  2. 【POI 2007】Ridges and Valleys山峰和山谷(GRZ)

    http://www.zybbs.org/JudgeOnline/problem.php?id=1102 八中题目描述太DT了--还是看这里: http://main.edu.pl/en/archiv ...

  3. 【搜索】训练题J-山峰和山谷 Ridges and Valleys

    题意 棋盘每个数字表示高度,四周高中间低为山谷,反之为山峰,要求统计山峰和山谷的数量 思路 枚举每个点,对每个点进行对8个方向跑bfs,判断该方块周围的高度情况. 代码 #include<ios ...

  4. [总结]2019年9月 OI学习/刷题记录

    从现在开始记录一下每天的学习情况.主力LOJ? 2019/9/5 LibreOJ #2543. 「JXOI2018」排序问题 答案显然是\(\frac{(n+m)!}{Cnt_1!Cnt_2!\cdo ...

  5. 【ybt高效进阶1-5-2】【luogu P3456】山峰和山谷 / GRZ-Ridges and Valleys

    山峰和山谷 / GRZ-Ridges and Valleys 题目链接:ybt高效进阶1-5-2 / luogu P3456 题目大意 对于山谷和山峰,我们这样定义: 它们是一个连通块(其中的高度都相 ...

  6. 广度优先搜索BFS-P3456 [POI2007]GRZ-Ridges and Valleys

    题目+提交地址 (这个题调了两个小时多,我是个蒟蒻QAQ) 题意:(正文题目的意思有点没有说明白,应该是8个方向,刚开始时我以为是四个方向,一直WA)找有多少个山峰和山谷,两个答案中间是空格隔开(因为 ...

  7. bzoj1108[POI2007]天然气管道Gaz*

    bzoj1108[POI2007]天然气管道Gaz 题意: n个钻井,n个站,要求两两配对,但站必须在钻井的右下方.配一对的费用为两点的曼哈顿距离,求最小总费用.n≤50000. 题解: 发现满足条件 ...

  8. BZOJ 1101: [POI2007]Zap

    题目 1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题: ...

  9. BZOJ1103: [POI2007]大都市meg

    题解:dfs序用树状数组维护即可 Problem: 1103User: c20161007Language: C++Result: AcceptedTime:4872 msMemory:23832 k ...

最新文章

  1. 远程桌面与本地桌面实现文件传输
  2. 使用Gson对复杂json对象的成员进行删选
  3. UBUNTU安装 Rabbitvsc可视化版本控制客户端软件
  4. XDP(eXpress Data Path)防御DDoS攻击
  5. html class和id,css教程之样式表的基本语法(二) class(类)和id的一个小实例
  6. windows下使用MinGW+msys编译ffmpeg
  7. java opencv orb_opencv python ORB算法
  8. 华为云大数据存储的冗余方式是三副本_华为TaurusDB技术解读(转载)
  9. Magento: 根据产品属性加载产品信息 Load A Category or Product by an Attribute
  10. 网络编程 之osi七层协议
  11. levy过程和布朗运动的关系_金融数学之定价模型基础解释【布朗运动|维纳过程|伊藤引理】...
  12. 有线与无线网络配置相关工具
  13. html5页面关闭的回调函数,js回调函数例子 js 回调函数问题的执行结果想作为返回值...
  14. 菜鸟电子面单对接记录
  15. 学好英语,才能当个好程序员,英语基础语法总结
  16. CNTV视频深入挖掘分析
  17. 恋爱测试题测男生软件,男友求生欲测试题大全
  18. java计算机毕业设计高校四六级报名管理系统源程序+mysql+系统+lw文档+远程调试
  19. บาคาร่า ธุรกิจที่สร้างรายได้ดี
  20. Python breakpoint()函数

热门文章

  1. 20个用户看了每天都想打开app的登录页面模板
  2. week05-继承、多态、抽象类与接口
  3. 最详细的Android Bitmap回收机制(从2.3到7.0,8.0)
  4. 笔记本电脑无法连接无线网问题
  5. Jenkins的详细安装及使用
  6. 如何用adobe audition剪切音频单独导出保存
  7. IO模型有哪些,讲讲你理解的nio ,他和bio,aio的区别是啥,谈谈reactor模型。
  8. matlab摩托车刹车问题,摩托车刹车你用对了吗?老司机都不一定会用后刹
  9. 算法可以用不同的语言描述如果用c语言,【判断题】算法可以用不同的语言描述,如果用C 语言或PASCAL语言等高级语言来描述,则算法实际上就是程序了。...
  10. linux桌面环境调整时钟,小技巧:Linux个性化面版时钟显示