POJ 1088 滑雪
来源:互联网 发布:天地通软件 编辑:程序博客网 时间:2024/05/16 06:14
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output
25
大体思路:
把高度从小到大进行排序,然后依次进行其上下左右点的比较。如果发现有比这个点高度高的,对比这个点高的点的距离进行更新。 例:if(a[x][y]<a[x-1][y]) a[x-1][y]=max(a[x][y]+1,a[x-1][y]) 默认距离为1,遍历完成后取其中距离最远的点。
代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;typedef struct { int x,y,h;}Node;Node maps[10010];//结构体数组进行排序bool cmp(Node a,Node b){ return a.h<b.h;}bool ismin(int x,int y){ if(x<y) return true; return false;}int main(){ int r,c,m=1; int a[102][102],len[102][102];//用a数组判断与上下左右的高度,Len数组记录相应的距离 while(cin>>r>>c) { for(int i=1;i<=r;++i){ for(int j=1;j<=c;++j){ cin>>a[i][j]; len[i][j]=1; maps[m].y=i; maps[m].x=j; maps[m].h=a[i][j]; m++; } } sort(maps+1,maps+r*c+1,cmp); for(int i=1;i<=r*c;++i){//依次遍历 int x=maps[i].x,y=maps[i].y; if(x>1)//进行更新,防止越界 if(ismin(a[y][x],a[y][x-1])) len[y][x-1]=max(len[y][x]+1,len[y][x-1]); if(x<c) if(ismin(a[y][x],a[y][x+1])) len[y][x+1]=max(len[y][x]+1,len[y][x+1]); if(y>1) if(ismin(a[y][x],a[y-1][x])) len[y-1][x]=max(len[y][x]+1,len[y-1][x]); if(y<r) if(ismin(a[y][x],a[y+1][x])) len[y+1][x]=max(len[y][x]+1,len[y+1][x]); } int maxn=0;//取最大 for(int i=1;i<=r*c;++i){ if(maxn<len[maps[i].y][maps[i].x]) maxn=len[maps[i].y][maps[i].x]; } cout<<maxn<<endl; } return 0;}
0 0
- POJ 1088 滑雪
- POJ 1088 滑雪
- POJ 1088 滑雪
- Poj 1088 滑雪
- poj 1088 滑雪
- 滑雪--POJ--1088
- poj 1088 滑雪(DP)
- POJ 1088 滑雪
- POJ 1088 滑雪
- poj 1088 滑雪
- POJ 1088 滑雪
- poj 1088 滑雪
- POJ 1088 滑雪
- poj 1088 滑雪
- poj 1088 滑雪
- POJ 1088 滑雪 DP
- POJ 1088 滑雪
- Poj 1088 滑雪
- Jquery日常运用
- VS2012的右侧滚动条不见了
- session和cookie讲述
- 监控系统nagios的安装
- 20170306-leetcode-202-Happy Number
- POJ 1088 滑雪
- android开发添加新的activity的注意事项
- 倒排索引原理和实现
- A. Petr and a calendar
- ASP模拟POST提交,然后XMLHTTP获取数据总是乱码,请大家帮忙,感谢!
- SVG 案例一
- 设计模式——观察者模式
- c#获取路径方式
- Haskell语言学习笔记(14)Foldable