poj 1088 滑雪 DP

本文介绍了一种算法,用于寻找给定二维数组中从任一点出发的最长下降路径长度。该算法采用动态规划方法,首先将数组内的元素按高度排序,然后依次遍历并更新路径长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个r*c 2维数组,包含高度,求最长下降子序列的长度,

每个节点只能移动上下左右四个格子。


dp[i][j]表示从第i,j格子开始的最长下降子序列长度。

先对数组内高度从低到高排序,之后按照高度从低到高 遍历 求dp即可。


测试数据:

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


1 1
1

1 2
1 2

1 3 
1 3 2

2 2
1 2 
4 3

2 2 
1 2 
3 4

4 7
7 6 5 4 3 2 1
1 5 1 1 1 1 1
1 4 3 1 1 1 1
1 5 6 7 8 1 1

3 3
9 1 2
5 6 7
8 4 3

3 4
1 2 3 4
8 7 6 5
9 10 11 12

3 3
0 0 0
0 5 0
0 0 0

12 13
1   1 30  4  800  6  7  8  99 10 1223 1
20 30 30 4 16 15 14 13 12 11 1
21 22 99 444444 88 9926 27 9928 9929 3000 456 1
40 39 1 90 36 35 34 33 3992 30001 789  1
41 42 4000 44  88 46 47 48 49 50 897  1
1 59 1 57 56  85 54 53 52 51 908 1
61 77 56 64 444 66 67 68 69 70 1234 1
80 79 78 77 76 75 74 73 72 71 12345 1
81 82 2  2 4 86 5 88 8 90 3456  1
100 99 98 97 96 95 94 93 92 91 567 1
890 654 623 154 683 15414 86549 633 123 456 123456  1
9517 45632 643164 3478643 43 16 431 64453132 689431 746546 15643 1
64543 13146543 13474 314789 4352154 65431 631 654324 65132 89547  34567312 1 1


13 12
1   1 30  4  800  6  7  8  99 10 1223 1
20 30 30 4 16 15 14 13 12 11 1
21 22 99 444444 88 9926 27 9928 9929 3000 456 1
40 39 1 90 36 35 34 33 3992 30001 789  1
41 42 4000 44  88 46 47 48 49 50 897  1
1 59 1 57 56  85 54 53 52 51 908 1
61 77 56 64 444 66 67 68 69 70 1234 1
80 79 78 77 76 75 74 73 72 71 12345 1
81 82 2  2 4 86 5 88 8 90 3456  1
100 99 98 97 96 95 94 93 92 91 567 1
890 654 623 154 683 15414 86549 633 123 456 123456  1
9517 45632 643164 3478643 43 16 431 64453132 689431 746546 15643 1
64543 13146543 13474 314789 4352154 65431 631 654324 65132 89547  34567312 1 1

3 3
0 1 2
1 0 1
2 1 0

3 3
0 0 0
0 0 0
0 0 0

1 1
0

10 10
1 2 300 4 5 6 7 8 9 10
20 19 18 17 16 15 14 13 12 11
21 22 23 24 25 26 27 28 29 30
40 39 38 37 36 35 34 33 32 31
41 42 43 44 45 46 47 48 49 50
60 59 58 57 56 55 54 53 52 51
61 62 63 64 65 66 67 68 69 70
80 79 78 77 76 75 74 73 72 71
81 82 83 84 85 86 87 88 89 90
100 99 98 97 96 95 94 93 92 91

4 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

4 4
1 2 2 1
1 4 4 1
1 3 3 1
1 2 2 1

3 3
9 1 2
5 6 7
8 4 3

/////////////////////////////////////////////////////////////////////////////

25
1
2
2
4
3
7
4
12
2
27
37
3
1
1
97
4
4
4

#include <iostream>
#include <iostream>
#include <algorithm>
#include <stdio.h>

#include <cstring>
using namespace std;

#define MAX_N (101*101)
struct point
{
	int r;
	int c;
	int h;
}POINT[MAX_N];
bool cmp(const point &a,const point &b)
{
	return a.h < b.h;
}

struct move{
	int di;
	int dj;
}MOVE[]={ {-1,0},{1,0},{0,1},{0,-1} };

int H[101][101];
int dp[101][101];

int pindex=1;
int R,C;

void dump()
{
	for(int i=1;i<=R;i++)
	{
		for(int j=1;j<=C;j++)
		{
			printf("dp[%d][%d] = [%d] \t",i,j,dp[i][j]);
		}
		printf("\n");
	}
}
int main()
{
	while(cin>>R>>C)
	{
		pindex=1;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=R;i++)
		{
			for(int j=1;j<=C;j++)
			{
				int h;
				cin>>h;

				POINT[pindex].r=i;
				POINT[pindex].c=j;
				POINT[pindex].h=h;
				pindex++;

				H[i][j]=h;
			}
		}

		sort(POINT+1,POINT+pindex,cmp);
		int maxret = 0;
		for(int i = 1;i<pindex;i++)
		{
			int r,c,h;

			r=POINT[i].r;
			c=POINT[i].c;
			h=POINT[i].h;

			int maxd = 0;
			for(int _m=0;_m< 4;_m++)
			{
				int di= MOVE[_m].di;
				int dj= MOVE[_m].dj;

				int ni = r+di;
				int nj = c+dj;

				if( 1<= ni && ni <= R && 1<=nj&& nj<= C)
				{
					if(h > H[ni][nj])
					{

						//int d = h - H[ni][nj] + dp[ni][nj];

						int d = dp[ni][nj] +1;
						if(d > maxd )
							maxd= d;

					}
				}
			}

			dp[r][c] = maxd;

			if(maxd > maxret)
				maxret = maxd;
		}

		//dump();
		
		cout<<maxret+1<<endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值