您的位置:首页 > 其它

hdu 5092 dp

2014-11-03 21:30 246 查看

Seam Carving

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 106 Accepted Submission(s): 62



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

Case 1
2 1 1 2
Case 2
3 2 1 1 2 1


[align=left]Source[/align]
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

//dp,类似于数塔,取上一层j-1,j,j+1中最小且最靠右的,路径的话,正向记录,反向输出
#include<stdio.h>
#define N 200
__int64 a

,dp

;
int f
,pre

;
int main()
{
int t,n,m,i,j,k,u,cnt=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%I64d",&a[i][j]);
for(i=1;i<=n;i++)
{
pre[1][i]=-1;
dp[1][i]=a[1][i];
}
for(i=2;i<=m;i++)
{
for(j=1;j<=n;j++)
{
u=j;
if(j>=2&&dp[i-1][j-1]<dp[i-1][u])
u=j-1;
if(j+1<=n&&dp[i-1][j+1]<=dp[i-1][u])
u=j+1;
dp[i][j]=a[i][j]+dp[i-1][u];
pre[i][j]=u;
}
}
u=1;
for(i=2;i<=n;i++)
if(dp[m][i]<=dp[m][u])
u=i;
i=m; k=1; f[0]=u;
while(pre[i][u]!=-1)
{
u=pre[i][u];
f[k++]=u;
i--;
}
printf("Case %d\n",cnt++);
for(i=k-1;i>0;i--)
printf("%d ",f[i]);
printf("%d\n",f[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: