HDU5092——DP+递归输出——Seam Carving

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

Source

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

大意:每行选一个,列与列之间距离不能超过1,输出从上到下连起来而且字典序最大的路径,字典序最大只要倒一下,学到了递归输出逆序方法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 110;
int dp[maxn][maxn];
int path[maxn][maxn];
int a[maxn][maxn];
int n,m;
const int inf = 0x3f3f3f3f;
void print(int x,int y)
{
    if(x == 1){
        printf("%d",y);
        return ;
    }
    print(x-1,path[x][y]);
    printf(" %d", y);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas = 1; cas <= T; cas++){
        memset(path,0,sizeof(path));
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&n,&m);
        for(int i = 1 ; i <= n ; i++){
            for(int j = 1; j <= m ;j++){
                dp[i][j] = inf;
                scanf("%d",&a[i][j]);
            }
        }
        for(int i = 1; i <= m ;i++)
            dp[1][i] = a[1][i];
        for(int i = 2; i <= n ; i++){
            for(int j = m; j >= 1 ; j--){
                for(int k = j+1; k >= j-1; k--){
                    if(k >= 1 && k <= m){
                        if(dp[i][j] > dp[i-1][k] + a[i][j]){
                            dp[i][j] = dp[i-1][k] + a[i][j];
                            path[i][j] = k;
                        }
                    }
                }
            }
        }
        int min1 = inf;
        int vis;
        for(int i = m; i >= 1 ;i--){
            if(dp[n][i] < min1){
                vis = i;
                min1 = dp[n][i];
            }
        }
        printf("Case %d\n",cas);
        print(n,vis);
        printf("\n");
    }
    return 0;
}

  

时间: 2024-08-29 19:32:28

HDU5092——DP+递归输出——Seam Carving的相关文章

递推DP HDOJ 5092 Seam Carving

题目传送门 1 /* 2 题意:从上到下,找最短路径,并输出路径 3 DP:类似数塔问题,上一行的三个方向更新dp,路径输出是关键 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <cstring> 9 #include <cmath> 10 #include <string> 11 #include <vector

uva 10453 Make Palindrome (区间DP + 递归输出)

uva 10453 Make Palindrome 题目大意:给出一段字符串,要求求出最少加入几个字符(任意位置),可以让该字符串变成会问字符串,并输出修改以后的回文字符串. 解题思路:dp[i][j]代表了将该字符串从第i位到第j位变成回文字符串最少要添加的字符.当S[i]==S[j],dp[i][j]=dp[i+1][j?1]当S[i]!=S[j],dp[i][j]=min(dp[i+1][j],dp[i][j?1])+1,在DP的过程中记录对该区间的操作类型,最后递归输出. #includ

hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 956    Accepted Submission(s): 382 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

hdu 5092 Seam Carving dp+记录路径

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 605    Accepted Submission(s): 253 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)

Seam Carving DescriptionFish 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

hdu 5092 Seam Carving(DP+记录路径)

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 119    Accepted Submission(s): 69 Problem Description Fish likes to take photo with his friends. Several days ago, he found that som

URAL 1183 Brackets Sequence DP 路径输出

题意:长度小于100的字符串s只由四种字符"()[]"组成,求以该串为子串的最短的合法串.合法串递归定义为: (1)空串合法 (2)如果S合法,则(S).[S]合法 (3)如果A.B合法,则AB合法 思路: 设dp[i][j]为s(i,j)变为合法串后,合法串的长度或需要添加的字符的个数,状态转移: (1)如果s[i]和s[j]匹配,dp[i,j]=dp[i+1,j-1]. (2)如果不匹配,划分s(i,j)为s(i,k)和s(k+1,j),划分后dp[i,j]=dp[i,k]+dp[

(review)zoj1276 区间dp+路径输出

[题解]:经典的区间dp,并且记录下了dp的path 因为是递归得到的path,所以递归压栈按从里到外的顺序得到path就可以了 输出嵌套括号部分很好的考察了对栈的理解,和递归执行的顺序. 注意题目输出中有的地方有空格 1 //zoj1276 路径输出用到了栈的思想,比较考验思维 2 #include<iostream> 3 #include<string.h> 4 #include<stdio.h> 5 #define maxn 13 6 using namespac

dp-LCS(递归输出最短合串)

Problem Description The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a