HDOJ 5092 Seam Carving DP
来源:互联网 发布:linux red hat使用教程 编辑:程序博客网 时间:2024/06/05 15:18
很基础的DP,每搞清楚尽量向右走具体是什么意思,调整了下循环的位置就过了。。。。
Seam Carving
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 89 Accepted Submission(s): 54
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.
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
Source
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int INF=0x3f3f3f3f;int a[110][110];int dp[110][110];int pre[110][110];int n,m;vector<int> road;void findroad(int d,int p){ if(d==1) return ; road.push_back(pre[d][p]); p=pre[d][p]; findroad(d-1,p);}int main(){ int T_T,cas=1; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); memset(dp,63,sizeof(dp)); memset(pre,0,sizeof(pre)); for(int i=1;i<=m;i++) dp[1][i]=a[1][i]; for(int i=2;i<=n;i++) { for(int j=m;j>=1;j--) { if(j+1<=m) { if(dp[i][j]>dp[i-1][j+1]+a[i][j]) { dp[i][j]=dp[i-1][j+1]+a[i][j]; pre[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]; pre[i][j]=j; } if(j-1>=1) { if(dp[i][j]>dp[i-1][j-1]+a[i][j]) { dp[i][j]=dp[i-1][j-1]+a[i][j]; pre[i][j]=j-1; } } } } int MP=INF,pos=-1; for(int i=m;i>=1;i--) { if(dp[n][i]<MP) { MP=dp[n][i]; pos=i; } } road.clear(); road.push_back(pos); findroad(n,pos); printf("Case %d\n",cas++); for(int i=road.size()-1;i>=0;i--) { printf("%d",road[i]); if(i) putchar(32); } putchar(10); } return 0;}
0 0
- HDOJ 5092 Seam Carving DP
- hdoj 5092 Seam Carving
- 【DP】 HDU 5092 Seam Carving
- hdu 5092 Seam Carving(dp)
- HDU 5092 Seam Carving DP
- Hdu 5092 Seam Carving(dp)
- HDU 5092 Seam Carving(DP, 路径)
- HDU 5092 Seam Carving(DP)
- hdu 5092 Seam Carving dp+记录路径
- hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】
- HDOJ 5092 Seam Carving(动态规划,回溯,记录路径)
- hdu 5092 Seam Carving
- hdu 5092 Seam Carving
- HDU 5092 Seam Carving
- HDU 5092 Seam Carving
- hdu 5092 Seam Carving(DP+记录路径)
- HDU 5092 Seam Carving(上海邀请赛基础DP)
- HDU 5092 Seam Carving (DP+打印路径)
- 食物API接口免费开放-APP数据接口
- iOS 开发 内存管理系列文章
- 游标cursor
- C语言逗号运算符
- Permutations II
- HDOJ 5092 Seam Carving DP
- Struts2拦截器的使用 (详解)
- ajax处理Json数据
- 基于android系统的加密文件系统设计
- Android获取时间戳,以及将时间戳转换为时间
- 关于APP接口设计
- Tomcat与Apache的区别
- FLASH实现ASP.NET MVC上传---Flash篇
- Oralce - 用户表空间迁移