二维的最长下降子序列
滑雪
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35399 | Accepted: 12399 |
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
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
</pre><pre class="sio" name="code" style="white-space: pre-wrap; word-wrap: break-word; line-height: 26px; background-color: rgb(255, 255, 255);">
<span style="font-family:FangSong_GB2312;font-size:12px;">题目意思很好理解。<span style="color: rgb(51, 51, 51); line-height: 13.3466672897339px;">给出一个二维数组,让你求出最长递减序列长度,可以四个方向行走,起点任意。</span></span> <span style="font-family:FangSong_GB2312;font-size:12px;color:#333333;"><span style="line-height: 13.3466672897339px;">思路跟一维的最长下降子序列问题非常相似,定义dp[i][j] = 以 height[i][j] 为起始点的最长下降子序列的长度。</span></span> <span style="font-family:FangSong_GB2312;font-size:12px;color:#333333;"><span style="line-height: 13.3466672897339px;">以height[i][j] 为起点的下降子序列有两种:</span></span> <span style="font-family:FangSong_GB2312;font-size:12px;color:#333333;"><span style="line-height: 13.3466672897339px;">1、只包含它自身的序列</span></span> <span style="font-family:FangSong_GB2312;font-size:12px;color:#333333;"><span style="line-height: 13.3466672897339px;">2、满足dp[i][j] = 它上下左右四个方向中的最大值的高度,当且仅当</span></span><span style="line-height: 13.3466672897339px; color: rgb(51, 51, 51); font-family: FangSong_GB2312;">它自身高度大于附近的高度。</span> 这样就可以构造出相应的状态方程。
<pre class="sio" name="code" style="white-space: pre-wrap; word-wrap: break-word; line-height: 26px; background-color: rgb(255, 255, 255);"><pre name="code" class="cpp">#include <cstdio> #include <iostream> #include<algorithm> #include <cstring> using namespace std; const int MAXN = 1005; int height[MAXN][MAXN]; int dp[MAXN][MAXN]; int R,C; int length; int DP(int i,int j) { int Max = 0; if (dp[i][j]>0) return dp[i][j]; //移动过程也可以开两个数组dx[],dy[],用循环来完成 if (i-1>=0) //向上移动 if(height[i][j] >height[i-1][j]) if (Max < DP(i-1,j)) Max = DP(i-1,j); if (i+1<R) //向下移动 if(height[i][j] >height[i+1][j]) if (Max < DP(i+1,j)) Max = DP(i+1,j); if (j-1>=0) //向左移动 if(height[i][j] >height[i][j-1]) if (Max < DP(i,j-1)) Max = DP(i,j-1); if (j+1<C) //向右移动 if(height[i][j] >height[i][j+1]) if (Max < DP(i,j+1)) Max = DP(i,j+1); dp[i][j] = Max + 1; return dp[i][j]; } int main() { while(cin>>R>>C) { memset(dp,0,sizeof(dp)); //处理输入的数据 for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) scanf("%d",&height[i][j]); //对每一段长度求对应的最长路线值 for(int i = 0; i < R; i++) for(int j = 0; j < C; j++) DP(i,j); cout<<*max_element(&dp[0][0],&dp[R][C])<<endl; } }
学了这么久的动态规划,感觉脑子渐渐开窍了……嘻,感觉还不错。