HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
Seam Carving
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*m的矩阵,求一条路径从第1到第n行使其经过的和最小,每个位置a[i][j]只能走三个点:a[i+1][j-1],a[i+1][j],a[i+1][j+1],打印出路径,即每行的列数。
题目要求路径要字典序最大。
解题思路:
dp[i][j]=max(dp[i-1][j]+data[i][j],dp[i-1][j-1]+data[i][j],dp[i-1][j+1]+data[i][j])
注意该题如果有相同大小的结果,输出最右的。
dp数组建立的时候应该按dp[i-1][j+1],dp[i-1][j],dp[i-1][j-1]的顺序以绝对大于来比较。这样可以保证最终结构为最右。
建立back[][]数组来保存路径。back[i][j]=a,表示dp[i][j]是使用第i-1行a列的数据构成的。
Code:
/*************************************************************************
> File Name: shanghai_1003.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月05日 星期三 16时14分02秒
************************************************************************/ #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=;
cin>>T;
while (T--)
{
memset(dp,,sizeof(dp));
memset(back,,sizeof(back));
cin>>M>>N;
for (int i=;i<=M;i++)
for (int j=;j<=N;j++){
scanf("%d",&data[i][j]);
dp[i][j]=;
}
for (int i=;i<=N;i++)
dp[][i]=data[][i];
for (int i=;i<=M;i++)
for (int j=;j<=N;j++)
{
if (j+<=N&&dp[i][j]>dp[i-][j+]+data[i][j])
dp[i][j]=dp[i-][j+]+data[i][j],back[i][j]=j+;
if (dp[i][j]>dp[i-][j]+data[i][j])
dp[i][j]=dp[i-][j]+data[i][j],back[i][j]=j;
if (j->=&&dp[i][j]>dp[i-][j-]+data[i][j])
dp[i][j]=dp[i-][j-]+data[i][j],back[i][j]=j-;
}
int min=,min_i=;
for (int i=N;i>=;i--){
if(min>dp[M][i]) min=dp[M][i],min_i=i;
}
printf("Case %d\n",times++);
for (int i=M;i>=;i--)
{
ans[i]=min_i;
min_i=back[i][min_i];
}
for (int i=;i<=M;i++)
{
printf("%d",ans[i]);
if(M==i) printf("\n");
else printf(" ");
} }
return ;
}
HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)的更多相关文章
- HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...
- HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)
Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...
- HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)
Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...
- Seam carving 学习笔记
今天首次接触了图像编辑中的seam carving知识,感觉挺神奇的.虽然我自己可能理解的不是很深刻,但是记录下来,总是好的. seam carving直接翻译过来是“线裁剪”的意思.它的主要用途是对 ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
- Programming Assignment 2: Seam Carving
编程作业二 作业链接:Seam Carving & Checklist 我的代码:SeamCarver.java 问题简介 接缝裁剪(Seam carving),是一个可以针对照片内容做正确缩 ...
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- 递推DP HDOJ 5092 Seam Carving
题目传送门 /* 题意:从上到下,找最短路径,并输出路径 DP:类似数塔问题,上一行的三个方向更新dp,路径输出是关键 */ #include <cstdio> #include < ...
- hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】
Seam Carving Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
随机推荐
- mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
安装Mysql后利用SQLyogEnt启动是提示“access denied for user 'root'@'localhost'(using password:YES)”,开始我还为是因为是密码问 ...
- Vim实用命令
[n]yy:从当前行复制n行 [n]p:粘贴n次 [n]dd:删除当前行往下的n行 / : 向后查找 ?:向前查找 u → undo 撤销上一操作 <C-r> → redo 0 → 开启 ...
- unity3d中的Viewport
Camera属性中有个Viewport Rect,如下图: X.Y为(0, 0)代表左下角,(1, 1)代表右上角:W和H分别是Viewport的宽(Width)和高(Height),摄像机的Aspe ...
- 我教女朋友学编程html系列(7)—Html无序列表、自定义列表、有序列表及常用例子
昨天写的那篇文章<我教女朋友学编程Html系列(6)—Html常用表单控件>,基本上有1000人左右看了,那边文章是我站在前人的肩膀上修改来的,添加了截图和说明,合并了例子,使之更容易被初 ...
- HDU 5695 Gym Class 拓扑排序
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5695 题解: 求出字典序最大的拓扑序.然后把求好的数列翻转过来就是满足条件的数列,然后模拟求一下va ...
- JS--传统事件模型的问题
事件绑定分为两种情况:传统的事件绑定(内联模型.脚本模型),一种是现代事件绑定模型(DOM2级事件绑定). 内联模型的事件绑定是将事件写在元素标签中,将事件绑定函数当做元素的一种属性来实现的,这种绑定 ...
- 设计模式之建造者模式(Builder)
建造者模式原理:建造模式主要是用于产生对象的各个组成部分,而抽象工厂模式则用于产生一系列对象,建造者模式而且要求这些对象的组成部分有序. 代码如下: #include <iostream> ...
- MVC 基础知识
一. MVC架构1.MVC模式是一种严格实现应用程序各部分隔离的架构模式.隔离:分离关注点,松耦合2.模型(Model) 代表着核心的业务逻辑和数据.模型封装了域实体的属性和行为3.视图(View) ...
- UML用例图(转载)
概述: 为了模拟系统最重要的方面是捕捉到的动态行为.为了阐明位详细信息,动态的行为意味着它运行时/操作系统的行为. 因此,只有静态的行为是不够的模拟系统,而动态的行为,更重要的是比静态行为.在UML模 ...
- 【补解体报告】topcoder 634 DIV 2
A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...