hdu 4826 Labyrinth(dp)

题目链接:hdu 4826 Labyrinth

题目大意:中文题。

解题思路:不难想的递推,dp[i][j][0]从上面过来的情况,dp[i][j][1]从下面过来的情况,然后这两种情况都可以从前一列走过来。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;
int n, m, g[N][N], dp[N][N][2];

void init () {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &g[i][j]);
}

int solve () {

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

    dp[0][1][0] = 0;
    for (int i = 1; i <= n; i++) {
        dp[i][1][0] = dp[i-1][1][0] + g[i][1];
        dp[i][1][1] = -INF;
    }

    for (int j = 2; j <= m; j++) {

        for (int i = 1; i <= n; i++)
            dp[i][j][0] = max(dp[i-1][j][0], max(dp[i][j-1][0], dp[i][j-1][1])) + g[i][j];

        for (int i = n; i >= 1; i--)
            dp[i][j][1] = max(dp[i+1][j][1], max(dp[i][j-1][0], dp[i][j-1][1])) + g[i][j];
    }
    return max(dp[1][m][0], dp[1][m][1]);
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int i = 1; i <= cas; i++) {
        init ();
        printf("Case #%d:\n%d\n", i, solve());
    }
    return 0;
}

hdu 4826 Labyrinth(dp),布布扣,bubuko.com

时间: 2024-12-21 12:40:50

hdu 4826 Labyrinth(dp)的相关文章

HDU 4826 简单DP

Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,且只能向上向下向右走以前没有走过的格子,每一个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币可以为负,需要给强盗写欠条),度度熊刚开始时身上金币数为0,问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T(T < 200),表示共有T组数据.每组数据的

HDU 4826 (分类DP)

Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1368    Accepted Submission(s): 574 Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,

hdu 4826 三维dp

dp的问题除了递推过程的设计之外 还有数据结构的选择以及怎样合理的填充数据 这个的填充是个坑..#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define INF -10000000 int mapp[105][105]; int dp[2][105][105]; int main() { int t,cnt=0;

百度之星资格赛 hdu 4826 Labyrinth 动态规划

/********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能走一格,且仅仅能向上向下向右走曾经没有走过的格子,每个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币能够为负,须要给强盗写欠条),度度熊刚開始时身上金币数为0.问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,