標籤:dp
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 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.
InputThere 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.
OutputFor 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
連結:http://acm.hdu.edu.cn/showproblem.php?pid=5092
題意:求一條路線,從矩陣的頂到矩陣的底,只能走到左下方,正下方,右下方。問總和最小的走法。 輸出從第一行到最後一行每行走的點的列標。要求相等的情況下,選最右邊的路線。
做法:dp就好了,讀懂題就不難了。因為路徑從第一行開始的,所以我把輸入的矩陣上下顛倒了。
然後除了第一行的的 dp值 賦值成輸入的以外,都初始化為INF。 然後轉移過來後,如果從右邊,就dir[i][r]=1, 中間0 ,左邊-1。注意要先更新右邊的,因為要選最右邊的路線。
然後dp好了從最後一行 中選出最小的,相等的話取最右邊的,然後遍曆到第一行,每次根據dir,改變列號,輸出就好了。
#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 個配 {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];dir[i][j]=1;}if(mid+num[i][j]<dp[i][j]){dp[i][j]=mid+num[i][j];dir[i][j]=0;}if(l+num[i][j]<dp[i][j]){dp[i][j]=l+num[i][j];dir[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++);printf("%I64d",id);for(__int64 i=n;i>=2;i--){id+=dir[i][id];printf(" %I64d",id);}puts(""); } return 0;}
連結:http://acm.hdu.edu.cn/showproblem.php?pid=5092
hdu 5092 Seam Carving dp+記錄路徑