hdu 5092 dp

<span style="font-family: 'Times New Roman'; font-size: 12px; background-color: rgb(255, 255, 255);">Seam Carving</span>

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 444    Accepted Submission(s): 196


Problem Description
Fish likes to take photo with his friends. Several days ago, he found that some pictures of him were damaged. The trouble is that there are some seams across the pictures. So he tried to repair these pictures. He scanned these pictures and stored them in his computer. He knew it is an effective way to carve the seams of the images He only knew that there is optical energy in every pixel. He learns the following principle of seam carving. Here seam carving refers to delete through horizontal or vertical line of pixels across the whole image to achieve image scaling effect. In order to maintain the characteristics of the image pixels to delete the importance of the image lines must be weakest. The importance of the pixel lines is determined in accordance with the type of scene images of different energy content. That is, the place with the more energy and the richer texture of the image should be retained. So the horizontal and vertical lines having the lowest energy are the object of inspection. By constantly deleting the low-energy line it can repair the image as the original scene.


For an original image G of m*n, where m and n are the row and column of the image respectively. Fish obtained the corresponding energy matrix A. He knew every time a seam with the lowest energy should be carved. That is, the line with the lowest sum of energy passing through the pixels along the line, which is a 8-connected path vertically or horizontally. 

Here your task is to carve a pixel from the first row to the final row along the seam. We call such seam a vertical seam.
 

Input
There several test cases. The first line of the input is an integer T, which is the number of test cases, 0<T<=30. Each case begins with two integers m, n, which are the row and column of the energy matrix of an image, (0<m,n<=100). Then on the next m line, there n integers.
 

Output
For each test case, print “Case #” on the first line, where # is the order number of the test case (starting with 1). Then print the column numbers of the energy matrix from the top to the bottom on the second line. If there are more than one such seams, just print the column number of the rightmost seam.
 

Sample Input
  
  
2 4 3 55 32 75 17 69 73 54 81 63 47 5 45 6 6 51 57 49 65 50 74 33 16 62 68 48 61 2 49 76 33 32 78 23 68 62 37 69 39 68 59 77 77 96 59 31 88 63 79 32 34
 

Sample Output
  
  
Case 1 2 1 1 2 Case 2 3 2 1 1 2 1
 

14年上海邀请赛的一道水水的dp,和经典的数塔模型几乎是一样的,直接从上往下d就可以了。唯一的两点不同:第一是在路径上所有数字的和相等的情况下,要选择最靠右的那条路,这个可以优先更新右边的点;第二是要输出路径,开一个pre路径,pre[i][j]表示点(i,j)的状态是从点(i-1,pre[i-1][pre[i][j]])继承过来的,详见代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 100000000
typedef long long LL;
LL dp[500][500] , a[500][500] , pre[500][500] , ans[500];
int main()
{
    LL n , m , T;
    scanf("%I64d", &T);
    LL Cas = 1;
    while(T--)
    {
        scanf("%I64d%I64d", &n, &m);
        for(LL i=1; i<=n; ++i)
        {
            for(LL j=1; j<=m; ++j)
            {
                scanf("%I64d", &a[i][j]);
            }
        }
        printf("Case %I64d\n", Cas);
        Cas++;
        memset(dp, 0, sizeof(dp));
        for(LL i=1; i<=m; ++i)
            dp[0][i] = a[0][i];
        for(LL i=0; i<=n; ++i)
            dp[i][0] = dp[i][m+1] = INF;
        for(LL i=1; i<=n; ++i)
            for(LL j=1; j<=m; ++j)
            {
                dp[i][j] = dp[i-1][j+1];///在选择三个点带来的效果相同的时候优先选择继承靠右的那个点
                pre[i][j] = j+1;
                if(dp[i][j] > dp[i-1][j])
                {
                    dp[i][j] = dp[i-1][j];
                    pre[i][j] = j;
                }
                if(dp[i][j] > dp[i-1][j-1])
                {
                    dp[i][j] = dp[i-1][j-1];
                    pre[i][j] = j-1;
                }
                dp[i][j] += a[i][j];
            }
   /**     for(LL i=1; i<=n; ++i)
        {
            for(LL j=1; j<=m; ++j)
            {
                prLLf("%I64d ", dp[i][j]);
            }
            prLLf("\n");
        }**/
        LL Min = INF;
        LL t;
        for(LL j=m; j>=1; --j) ///从右往左找和最小的路径
        {
            if(dp[n][j] < Min)
            {
                Min = dp[n][j];
                t = j;
            }
        }
        ans[0] = t;
        LL k = 1;
        for(LL i=n; i>1; --i) ///从最后一个点顺着pre数组一直往前找,找到路径
        {
            ans[k++] = pre[i][t];
            t = pre[i][t];
        }
        printf("%I64d", ans[n-1]);
        for(LL i=n-2; i>=0; --i)
            printf(" %I64d", ans[i]);
        printf("\n");
    }
    return 0;
}
另外说一点,本题用记忆化搜索也特别好做,有兴趣的可以试试,直接往回回朔输出就好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值