HDOJ 5092 Seam Carving(動態規劃,回溯,記錄路徑)

Seam Carving

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


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
題意:從第一行到第n行使其路徑最小,輸出最短路的列下標,當其在a[i][j]點時,下一步可以向a[i+1][j],a[i+1][j-1],a[i+1][j+1]三個方向,所以我們可以得到動態轉移方程:dp[i][j]=min(a[i+1][j],a[i+1][j-1],a[i+1][j+1]);並用一個vis[i][j]數組記錄下一行的最小點的下標。
代碼:
代碼一:從上往下遍歷:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define mod 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int vis[101][101],a[101][101],dp[101][101];
int main()
{
    int T;
    scanf("%d", &T);
    for(int t=1; t<=T; t++)
    {
        int n,m;
        scanf("%d %d", &n, &m);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)	scanf("%d", &a[i][j]);
        }
        for(int j=1; j<=m; j++)	dp[n][j] = a[n][j];
        for(int i=n-1; i>=1; i--)
        {
            for(int j=1; j<=m; j++)
            {
                dp[i][j]=dp[i+1][j];
                vis[i][j]=j;
                if(dp[i][j]>dp[i+1][j-1]&&j-1>=1&&i+1<=n)
                {
                    dp[i][j]=dp[i+1][j-1];
                    vis[i][j]=j-1;
                }
                if(dp[i][j]>=dp[i+1][j+1]&&j+1<=m&&i+1<=n)
                {
                    dp[i][j]=dp[i+1][j+1];
                    vis[i][j]=j+1;
                }
                dp[i][j]+=a[i][j];
            }
        }
        int ans = mod;
        int k;
        for(int i=1; i<=m; i++)
        {
            if(dp[1][i]<=ans)
            {
                ans = dp[1][i];
                k= i;
            }
        }
        printf("Case %d\n%d", t, k);
        for(int i=1; i<n; i++)
        {
            k = vis[i][k];
            printf(" %d", k);
        }
        printf("\n");
    }
    return 0;
}

從下往上遍歷:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 1000
using namespace std;
int back[MAXN][MAXN],dp[MAXN][MAXN],data[MAXN][MAXN];
int ans[MAXN],M,N;
int main()
{
    int T,times=1;
    cin>>T;
    while (T--)
    {
        memset(dp,0,sizeof(dp));
        memset(back,0,sizeof(back));
        cin>>M>>N;
        for (int i=1;i<=M;i++)
            for (int j=1;j<=N;j++){
                scanf("%d",&data[i][j]);
                dp[i][j]=1000000;
            }
        for (int i=1;i<=N;i++)
            dp[1][i]=data[1][i];
        for (int i=2;i<=M;i++)
            for (int j=1;j<=N;j++)
            {
                if (j+1<=N&&dp[i][j]>dp[i-1][j+1]+data[i][j])
                    dp[i][j]=dp[i-1][j+1]+data[i][j],back[i][j]=j+1;
                if (dp[i][j]>dp[i-1][j]+data[i][j])
                    dp[i][j]=dp[i-1][j]+data[i][j],back[i][j]=j;
                if (j-1>=1&&dp[i][j]>dp[i-1][j-1]+data[i][j])
                    dp[i][j]=dp[i-1][j-1]+data[i][j],back[i][j]=j-1;
            }
        int min=1000000,min_i=0;
        for (int i=N;i>=1;i--){
            if(min>dp[M][i]) min=dp[M][i],min_i=i;
        }
        printf("Case %d\n",times++);
        for (int i=M;i>=1;i--)
        {
            ans[i]=min_i;
            min_i=back[i][min_i];
        }
        for (int i=1;i<=M;i++)
        {
            printf("%d",ans[i]);
            if(M==i) printf("\n");
            else printf(" ");
        }

    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章