码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-05 19:26:39      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   使用   for   

Seam Carving


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

题目大意:

    给你一个n*m的矩阵,求一条路径从第1到第n行使其经过的和最小,每个位置a[i][j]只能走三个点:a[i+1][j-1],a[i+1][j],a[i+1][j+1],打印出路径,即每行的列数。
    题目要求路径要字典序最大。

解题思路:

    dp[i][j]=max(dp[i-1][j]+data[i][j],dp[i-1][j-1]+data[i][j],dp[i-1][j+1]+data[i][j])

    注意该题如果有相同大小的结果,输出最右的。

    dp数组建立的时候应该按dp[i-1][j+1],dp[i-1][j],dp[i-1][j-1]的顺序以绝对大于来比较。这样可以保证最终结构为最右。

    建立back[][]数组来保存路径。back[i][j]=a,表示dp[i][j]是使用第i-1行a列的数据构成的。

Code:

 1 /*************************************************************************
 2     > File Name: shanghai_1003.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com
 5     > Created Time: 2014年11月05日 星期三 16时14分02秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 1000
23 using namespace std;
24 int back[MAXN][MAXN],dp[MAXN][MAXN],data[MAXN][MAXN];
25 int ans[MAXN],M,N;
26 int main()
27 {
28     int T,times=1;
29     cin>>T;
30     while (T--)
31     {
32         memset(dp,0,sizeof(dp));
33         memset(back,0,sizeof(back));
34         cin>>M>>N;
35         for (int i=1;i<=M;i++)
36             for (int j=1;j<=N;j++){
37                 scanf("%d",&data[i][j]);
38                 dp[i][j]=1000000;
39             }
40         for (int i=1;i<=N;i++)
41             dp[1][i]=data[1][i];
42         for (int i=2;i<=M;i++)
43             for (int j=1;j<=N;j++)
44             {
45                 if (j+1<=N&&dp[i][j]>dp[i-1][j+1]+data[i][j])
46                     dp[i][j]=dp[i-1][j+1]+data[i][j],back[i][j]=j+1;
47                 if (dp[i][j]>dp[i-1][j]+data[i][j])
48                     dp[i][j]=dp[i-1][j]+data[i][j],back[i][j]=j;
49                 if (j-1>=1&&dp[i][j]>dp[i-1][j-1]+data[i][j])
50                     dp[i][j]=dp[i-1][j-1]+data[i][j],back[i][j]=j-1;
51             }
52         int min=1000000,min_i=0;
53         for (int i=N;i>=1;i--){
54             if(min>dp[M][i]) min=dp[M][i],min_i=i;
55         }
56         printf("Case %d\n",times++);
57         for (int i=M;i>=1;i--)
58         {
59             ans[i]=min_i;
60             min_i=back[i][min_i];
61         }
62         for (int i=1;i<=M;i++)
63         {
64             printf("%d",ans[i]);
65             if(M==i) printf("\n");
66             else printf(" ");
67         }
68 
69     }
70     return 0;
71 }

 

    

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

标签:des   style   blog   io   color   ar   os   使用   for   

原文地址:http://www.cnblogs.com/Enumz/p/4076979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!