POJ 1088 滑雪
2016-08-20 20:23
288 查看
滑雪Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
SubmitStatusPracticePOJ
1088
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为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
Sample Output
集体报告:一开始以为是dfs,结果超时,后来发现可以打表,用dp。 一个点一个点的求该点的最长底滑坡,填表。注意:可以不考虑当高度有0的情况。
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<stack>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef long long ll;
const int MAX=105;
int a[MAX][MAX];
int sum[MAX][MAX]; //记录a(i,j)的最长距离;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int r,c;
int dp(int x,int y){
if(sum[x][y]!=0) //可能求之前的数时一并求了
return sum[x][y];
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0||xx>=r||yy<0||yy>=c||a[xx][yy]>=a[x][y])
continue;
int n=1;
n+=dp(xx,yy);
if(n>sum[x][y])
sum[x][y]=n;
}
return sum[x][y];
}
int main(){
// freopen("input.txt","r",stdin);
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%d",&a[i][j]);
memset(sum,0,sizeof(sum));
int maxx=0;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
int k=dp(i,j);
if(k>maxx)
maxx=k;
}
printf("%d\n",maxx+1);
return 0;
}
& %llu
SubmitStatusPracticePOJ
1088
Description
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
集体报告:一开始以为是dfs,结果超时,后来发现可以打表,用dp。 一个点一个点的求该点的最长底滑坡,填表。注意:可以不考虑当高度有0的情况。
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<stack>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef long long ll;
const int MAX=105;
int a[MAX][MAX];
int sum[MAX][MAX]; //记录a(i,j)的最长距离;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int r,c;
int dp(int x,int y){
if(sum[x][y]!=0) //可能求之前的数时一并求了
return sum[x][y];
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0||xx>=r||yy<0||yy>=c||a[xx][yy]>=a[x][y])
continue;
int n=1;
n+=dp(xx,yy);
if(n>sum[x][y])
sum[x][y]=n;
}
return sum[x][y];
}
int main(){
// freopen("input.txt","r",stdin);
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%d",&a[i][j]);
memset(sum,0,sizeof(sum));
int maxx=0;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
int k=dp(i,j);
if(k>maxx)
maxx=k;
}
printf("%d\n",maxx+1);
return 0;
}
相关文章推荐
- [POJ] 1088 -> 滑雪
- POJ1088 滑雪
- POJ 1088 滑雪
- POJ - 1088 滑雪 DP+记忆化搜索
- POJ-1088滑雪,典型的动态规划题,与NYOJ-10skiing一样,但NYOJ上时限是3s,用搜索可以过,但在POJ上就超时了~~
- poj 1088 滑雪(贪心算法)
- poj-1088-滑雪【记忆化搜索+DP】
- poj1088 滑雪 动态规划+回溯
- poj1088-滑雪-记忆化搜索
- POJ 1088 滑雪
- Poj_1088_滑雪(DP)
- poj 1088 滑雪
- (DP6.1.4.2)POJ 1088 滑雪
- poj 1088/nyoj 10 滑雪(记忆化搜索/DP)
- POJ 1088 滑雪
- POJ1088 滑雪
- POJ 1088 滑雪(记忆化搜索)
- poj1088 滑雪(dfs、dp优化)
- POJ 1088 滑雪
- POJ 1088 滑雪(DP+记忆化搜索)