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

来源:互联网 发布:大数据职称 编辑:程序博客网 时间:2024/06/06 01:39

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 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
24 355 32 7517 69 7354 81 6347 5 456 651 57 49 65 50 7433 16 62 68 48 612 49 76 33 32 7823 68 62 37 69 3968 59 77 77 96 5931 88 63 79 32 34
 

Sample Output
Case 12 1 1 2Case 23 2 1 1 2 1
 


 题意:给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并输出这条线,线能向8个方向

延伸,要求找的是纵向的一条线(每一行各取一个点连成一线) ,输出路径,若有多条路径,输出最

靠右的。


#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 110;int dp[maxn][maxn],path[maxn][maxn],a[maxn][maxn],n,m;void initial(){    memset(path,-1,sizeof(path));    for(int i=0; i<maxn; i++)        for(int j=0; j<maxn; j++)            dp[i][j]=inf;}void input(){    scanf("%d %d",&n,&m);    for(int i=1; i<=n; i++)        for(int j=1; j<=m; j++)            scanf("%d",&a[i][j]);}void print(int num,int index){     if(num<1)  return ;     if(path[num][index]==-1)     {          printf("%d ",index);          return ;     }     else     {          print(num-1,path[num][index]);          printf("%d ",index);     }}void solve(int co){    printf("Case %d\n",co);    for(int j=1; j<=m; j++)  dp[1][j]=a[1][j];    for(int i=2; i<=n; i++)        for(int j=1; j<=m; j++)        {            if(j!=1 && dp[i][j]>dp[i-1][j-1]+a[i][j])            {                dp[i][j]=dp[i-1][j-1]+a[i][j];                path[i][j]=j-1;            }            if(dp[i][j]>=dp[i-1][j]+a[i][j])            {                dp[i][j]=dp[i-1][j]+a[i][j];                path[i][j]=j;            }            if(j!=m && dp[i][j]>=dp[i-1][j+1]+a[i][j])            {                dp[i][j]=dp[i-1][j+1]+a[i][j];                path[i][j]=j+1;            }        }    int Min=inf,index=-1;    for(int j=m;j>=1;j--)    {        if(Min>dp[n][j])        {            Min=dp[n][j];            index=j;        }    }    print(n-1,path[n][index]);    printf("%d\n",index);}int main(){    int T;    scanf("%d",&T);    for(int co=1; co<=T; co++)    {        initial();        input();        solve(co);    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶本扣分满了怎么办 摩托车被扣12分怎么办 驾照被扣12分怎么办 骑摩托被扣12分怎么办 摩托车没有驾照违章被扣分怎么办 高考登录密码丢了怎么办 高中档案遗失里面体检表怎么办 驾校不给补科一成绩单怎么办 外地驾照转广州体检表怎么办 驾照体检表签名签错怎么办 改完名字后护照怎么办 办健康证没有身份证怎么办 身份证掉了怎么办健康证 华师附小不搬了怎么办 健康证快到期了怎么办 老公想去日本打工怎么办 学生没有资产证明怎么办日本签证 在读证明学校不按模版怎么办 办日本签证没有户口本怎么办 日本大学留级续签失败怎么办 法国签证递交时间太晚怎么办 日本留学生签证更新拒签怎么办 永驻拒签了我该怎么办 越南签证拒签了怎么办 l1签证续签被拒怎么办 去日本跟团签证怎么办 手表里指针掉了怎么办 北京居住证过期半年了怎么办 居住证明居委会不盖章怎么办 小孩感冒鼻子不通气怎么办 1岁宝宝喉咙痰多怎么办 两个月喉咙有痰怎么办 10个月咳嗽有痰怎么办 昆山初级会计审核没有居住证怎么办 初级会计审核没有居住证怎么办 哈尔滨原房主户口不牵走怎么办 在亲戚家住怎么办居住证 按揭车押金不退怎么办 护照到期韩国签证没到期怎么办 日本签证银行流水不够怎么办 想去日本cm签证怎么办