dp
2013-07-07 14:46
288 查看
Problem Description
爱编程的小明同样也爱滑雪。为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。小明想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子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
#include<iostream> using namespace std; int n,m; int map[1005][1005]; int dp[1005][1005]; int dir[4][2]={0,1,0,-1,1,0,-1,0}; int dfs(int x,int y) { if(dp[x][y]) return dp[x][y]; int max=0; for(int i=0;i<4;i++) { int nx=x+dir[i][0],ny=y+dir[i][1]; if(nx==0||ny==0||nx==n+1||ny==m+1) continue; if(map[nx][ny]>=map[x][y]) continue; max=max>dfs(nx,ny)?max:dfs(nx,ny); } dp[x][y]=max+1; return max+1; } int main() { int ans; while(cin>>n>>m) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>map[i][j]; memset(dp,0,sizeof(dp)); ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans=ans>dfs(i,j)?ans:dfs(i,j); cout<<ans<<endl; } return 0; }
相关文章推荐
- hdu 4267 树形dp
- poj 3046 Ant Counting (DP多重背包变形)
- lightoj 1158 状态压缩+数位DP
- poj1458 dp
- [dp uestc oj] G - 邱老师玩游戏
- POJ 题目3744(矩阵优化概率DP)
- POJ 3666 Making the Grade【DP】
- HDU 1561(树形dp)
- 数塔取数 基础dp
- 【数位dp】HDU 3652 B-number
- hdu1074 状态压缩dp 记录路径
- [DP] HDU 2859
- HDU 2196--Computer(树形dp)
- UVAlive 6131 dp+斜率优化
- Android - 小功能 - Android中dp和px之间进行转换
- dp和px的转化
- UESTC 2015dp专题 E 菲波拉契数制 dp
- 概率统计(DP)
- HDU 1054.Strategic Game【动态规划、DP】【4月7】
- HDU 4283 You Are the One(区间DP)