ACM学习感悟——HDU5092(dp)
来源:互联网 发布:淘宝生活服务类目推广 编辑:程序博客网 时间:2024/05/22 15:35
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其实这道题是一个比较经典的数塔类型的DP,状态转移方程也很好写,就是从这一格上面的三个转移过来,唯一的坑点就是要求输出最右边的边,那么在专一的时候,那个等于号就很重要。因为要打印路劲,所以不能直接用min函数来转移。转移方程:d[i][j]=min(d[i-1][j-1],d[i-1][j],d[i-1][j+1])+a[i][j]另外考虑边界情况。AC代码:// Created by CQUWEL // Copyright (c) 2015年 CQUWEL. All rights reserved. #include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib> #include <algorithm>#include <cctype>#include <stack>#include <queue>#include <map>#include <string>#include <set>#include <vector>#define INF 0x3f3f3f3f#define cir(i,a,b) for (int i=a;i<=b;i++)#define CIR(j,a,b) for (int j=a;j>=b;j--)#define CLR(x) memset(x,0,sizeof(x))typedef long long ll;using namespace std;int T;int m,n;int a[110][110];int d[110][110];int pre[110][110]; int main(){//freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);cin >> T;for (int test=1;test<=T;test++){cin >> m >> n;for (int i=1;i<=m;i++)for (int j=1;j<=n;j++)cin >> a[i][j];for (int i=1;i<=n;i++)d[1][i]=a[1][i];for (int i=2;i<=m;i++)for (int j=1;j<=n;j++)d[i][j]=INF;memset(pre,-1,sizeof(pre));//------初始化 --------------for (int i=2;i<=m;i++) for (int j=1;j<=n;j++) { if (j-1>=1 && j+1<=n) { for (int k=j-1;k<=j+1;k++) { if (a[i][j]+d[i-1][k]<=d[i][j]) //这里的等于号非常重要。 { d[i][j]=a[i][j]+d[i-1][k]; pre[i][j]=k; } } } else if (j-1<1) { for (int k=j;k<=j+1;k++) { if (a[i][j]+d[i-1][k]<=d[i][j]) { d[i][j]=a[i][j]+d[i-1][k]; pre[i][j]=k; } } } else if (j+1>n) { for (int k=j-1;k<=j;k++) { if (a[i][j]+d[i-1][k]<=d[i][j]) { d[i][j]=a[i][j]+d[i-1][k]; pre[i][j]=k; } } } }//cout << pre[2][1] << endl;/*cout << "--------------------------\n";for (int i=1;i<=m;i++){for (int j=1;j<=n;j++)cout << pre[i][j] << " ";cout << endl;}cout << "--------------------------\n";*/ int ans=INF; int flag; for (int i=1;i<=n;i++)if (ans>=d[m][i]){ans=d[m][i];flag=i;} deque<int> q; q.push_front(flag); int ss=m,tt=flag; while(pre[ss][tt]!=-1) { q.push_front(pre[ss][tt]);tt=pre[ss][tt];ss--; } cout << "Case " << test << endl; int l=q.size(); while (!q.empty()) { int x=q.front(); printf("%d",x); if (l!=1) cout << " "; else cout << endl; q.pop_front(); l--; }// cout << endl;}return 0;}
0 0
- ACM学习感悟——HDU5092(dp)
- ACM学习感悟——暴力专场F(dp)
- ACM学习感悟——暴力专场E 暴力dp
- ACM学习感悟——POJ1932(图论)
- ACM学习感悟——POJ3169(差分约束)
- ACM学习感悟——POJ2139(Floyd)
- ACM学习感悟——POJ1258(kruskal)
- ACM学习感悟——暴力专场B(dfs)
- ACM学习感悟——FZU2192(并查集)
- ACM学习感悟——HDU-命运
- ACM 学习感悟——HDU2845
- ACM学习感悟——HDU1204
- ACM学习感悟——weekly training
- HDU5092 Seam Carving(2014上海全国邀请赛——题目重现)(DP)
- HDU5092
- hdu5092
- ACM学习感悟——HDU257 HOW TO TYPE
- ACM学习感悟——HDU2844 多重背包
- 数据库中自增量字段和Guid字段的比较
- Installing JBoss Application Server 7.1 on Mac OS X with Eclipse Integration
- CentOS7.0上Jenkins的使用
- HTTP协议模拟登录网站
- android位置传感器
- ACM学习感悟——HDU5092(dp)
- PHP设计模式——适配器模式
- JSP:setPropery ()、getProperty(){两个页面}
- Ubuntu14.04下搭建可远程访问的Mysql服务器
- 求出部分有序数组中的最大子数组(第一次出现)的长度及位置 (c++实现)
- 【字符串】字符串包含问题的最优解
- hdu1575 Tr A 矩阵快速幂
- SGU - 106 - The equation (扩展欧几里得)
- Linux内核分析课程8_进程调度与进程切换过程