HDU 5092 Seam carving dp+ record path

Source: Internet
Author: User

Seam CarvingTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 605 Accepted Submission (s): 253


Problem Descriptionfish likes to take photo with his friends. Several days ago, he found that some pictures of him were damaged. The trouble is, there is 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-on-the-carve the seams of the images He only knew that there was 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 s Caling effect. In order to maintain the characteristics of the image pixels to delete the importance of the image lines must is weakest. The importance of the pixel lines is determined in accordance with the type of scene images of different energy content. That's, the place with the + and the richer texture of the image should be retained. So the horizontal and vertical lines have 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 a original image G of M*n, where M and N is 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's, the line and the lowest sum of energy passing through the pixels along the line, which is a 8-connected path ver Tically or horizontally.

Here your task was to carve a pixel from the first row to the final row along the seam. We call such seam a vertical seam.
Inputthere several test cases. The first line of the input is a integer T, which is the number of test cases, 0<t<=30. Each case begins with integers m, n, which is the row and column of the energy matrix of an image, (0<m,n<=100) . Then on the next m line, there n integers.
Outputfor each test case, print ' Case # ' on the first line, where # was the order number of the the test case (starting with 1) . Then print the column numbers of the "The Energy Matrix" from the top to the bottom on the second line. If there is 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 59 31 88 63 79 32 34

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




Links: http://acm.hdu.edu.cn/showproblem.php?pid=5092

Test instructions: Ask for a route, from the top of the matrix to the bottom of the matrix, only to the lower left, directly below, right below.  Ask the sum of the smallest walk. Prints the column labels for each point of the walk from the first line to the last row. If the requirements are equal, select the right-most route.


Practice: DP is good, read the question is not difficult. Because the path starts from the first line, I invert the input matrix up and down.


Then, except for the first line of the DP value assigned to the input, it is initialized to the INF. Then transfer over, if from the right, on Dir[i][r]=1, Middle 0, left-1. Note To update the right side first, because you want to select the right-most route.


Then DP good from the last line to select the smallest, equal to take the rightmost, and then traverse to the first line, each time according to Dir, change the column number, the output is good.



#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 999999999999;__int64 Num[110][110];__int64    Dp[110][110];__int64 Dir[110][110];int Main () {__int64 t;__int64 n,m;__int64 cas=1;scanf ("%I64d", &t); while (t--)//k with {scanf ("%i64d%i64d", &n,&m), for (__int64 i=1;i<=n;i++) {for (__int64 j=1;j<=m;j++) { scanf ("%i64d", &num[n-i+1][j]);}} for (__int64 i=0;i<=n;i++) {for (__int64 j=0;j<=m+1;j++) {dp[i][j]=inf;}} for (__int64 j=1;j<=m;j++) dp[1][j]=num[1][j];for (__int64 i=2;i<=n;i++) {for (__int64 j=1;j<=m;j++) {__int64 L =dp[i-1][j-1];__int64 Mid=dp[i-1][j];__int64 r=dp[i-1][j+1];if (R+num[i][j]<dp[i][j]) {dp[i][j]=R+NUM[I][J];d ir[i][j]=1;} if (Mid+num[i][j]<dp[i][j]) {dp[i][j]=mid+num[i][j];d ir[i][j]=0;} if (L+num[i][j]<dp[i][j]) {dp[i][j]=l+num[i][j];d ir[i][j]=-1;}}} __int64 Minn=inf;__int64 id=-1;for (__int64 j=m;j>=1;j--) {if (Dp[n][j]<minn) {minn=dp[n][j];id=j;}} printf ("Case%i64d\n", cas++);p rintf ("%i64d", id), a for (__int64 i=n;i>=2;i--) {Id+=dir[i][id];p rintf ("%i64d", id);}    Puts ("");  } return 0;}





Links: http://acm.hdu.edu.cn/showproblem.php?pid=5092

HDU 5092 Seam carving dp+ record path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.