hdu 5092 Seam Carving 简单DP ”水一炮试试“大法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5092

非常卡读题

题目中说可以八个方向地走,完全没有提及是从上往下的

需要从样例中猜测”可能只是从上往下“,然后根据现场的过题情况决定要不要水一发试试

对于”水一炮试试“,感觉一般适用于:

1.本题的其他做法未果/很难写,其他的题目没法出

2.码的成本不会很高

3.心态上,得之我幸,失之我命

(水不过的时候,再检查一下水的姿势有没有什么不妥,如果还是不行,就要勇敢地、果断地走出过不了题的不开心~)

所以真正的题意是

只能从上往下走,8方向相邻

求权值和最小的路径 输出路径

如果有多条路径同时满足则输出最右的一条

教科书例题级别的DP

#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int maxn = 110;
const int INF = 0x7fffffff;

int a[maxn][maxn];
int dp[maxn][maxn];
int pre[maxn][maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    int kase = 0;
    while(T--)
    {
        printf("Case %d\n", ++kase);

        int m, n;
        scanf("%d%d", &m, &n);

        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d", &a[i][j]);

        for(int i = 1; i <= m; i++)
            dp[i][0] = dp[i][n+1] = INF;

        for(int j = 1; j <= n; j++)
            dp[1][j] = a[1][j];

        for(int i = 2; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(dp[i-1][j-1] < dp[i-1][j] && dp[i-1][j-1] < dp[i-1][j+1]) //必须要严格小于才选最左边
                {
                    dp[i][j] = dp[i-1][j-1] + a[i][j];
                    pre[i][j] = j-1;
                }
                else if(dp[i-1][j] < dp[i-1][j+1])
                {
                    dp[i][j] = dp[i-1][j] + a[i][j];
                    pre[i][j] = j;
                }
                else
                {
                    dp[i][j] = dp[i-1][j+1] + a[i][j];
                    pre[i][j] = j+1;
                }
            }
        }

        int minloc = 0;
        for(int i = 1; i <= n; i++)
            if(dp[m][i] <= dp[m][minloc])
                minloc = i;

        stack<int> st;
        for(int i = m; i >= 1; i--)
        {
            st.push(minloc);
            if(i != 1)
                minloc = pre[i][minloc];
        }

        for(int i = 0; i < m; i++)
        {
            printf("%d%c", st.top(), " \n"[i == m-1]);
            st.pop();
        }
    }

    return 0;
}
时间: 2024-11-04 05:18:16

hdu 5092 Seam Carving 简单DP ”水一炮试试“大法的相关文章

hdu 5092 Seam Carving(DP+记录路径)

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 119    Accepted Submission(s): 69 Problem Description Fish likes to take photo with his friends. Several days ago, he found that som

hdu 5092 Seam Carving dp+记录路径

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 605    Accepted Submission(s): 253 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

hdu 5092 Seam Carving (简单数塔DP,题没读懂,,不过可以分析样例)

题意: 给一个m*n的矩阵,每格上有一个数. 找从第1行到第m行的一条路径,使得这条路径上的数之和最小. 路径必须满足相邻两行所选的两个数的纵坐标相邻(即一个格子必须是另一个格子的周围八个格子中的一个) 输出每一行取的数的列值.  若有多个答案,则路径要求尽量靠右. 思路: 简单数塔DP.题比较不好读,不过可以分析样例. 代码: int T,m,n; int a[105][105], f[105][105]; int path[105]; int main(){ cin>>T; rep(t,1

hdu 5092 Seam Carving

这道题 我没看出来 他只可以往下走,我看到的 8-connected :所以今天写一下如果是 8-connected 怎么解: 其实说白了这个就是从上到下走一条线到达最后一行的距离最小: 从Map[a][b] 到Map[a][b+1] 的距离是Map[a][b+1] 以此类推:建图即可: 然后在加一个点0,和n+m+1 点这样在建立一下从  0 点到第一行的边,和最后一行到(n+m+1) 的边 求一个从0 到(n+m+1) 的最短路径就好了, 怎么维护最右侧?:  Dijkstra  有 队列优

递推DP HDOJ 5092 Seam Carving

题目传送门 1 /* 2 题意:从上到下,找最短路径,并输出路径 3 DP:类似数塔问题,上一行的三个方向更新dp,路径输出是关键 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <cstring> 9 #include <cmath> 10 #include <string> 11 #include <vector

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 956    Accepted Submission(s): 382 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

hdu 2084 数塔 (简单dp)

http://acm.hdu.edu.cn/showproblem.php?pid=2084 数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 28668    Accepted Submission(s): 17230 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下

Max Sum (hdu 1003 简单DP水过)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 161294    Accepted Submission(s): 37775 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s