• hihocoder #1617 : 方格取数(dp)


    题目链接:http://hihocoder.com/problemset/problem/1617

    题解:一道递推的dp题。这题显然可以考虑两个人同时从起点出发这样就不会重复了设dp[step][i][j]表示走了step步,第一个人在第i行第二个人在第j行第几列就用step减去就行

    然后就是简单的递推注意第一个人一定是在第二个人上面的这样才确保不会重复。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define inf 0X3f3f3f3f
    using namespace std;
    int dp[2 * 233][233][233];
    int a[233][233] , n;
    bool Is(int step , int x , int y) {
        int x1 = step - x , y1 = step - y;
        return (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && x >= 0 && x < n && y >= 0 && y < n);
    }
    int get_val(int step , int x , int y) {
        if(Is(step , x , y)) return dp[step][x][y];
        return -inf;
    }
    int main() {
        scanf("%d" , &n);
        for(int i = 0 ; i < n ; i++) {
            for(int j = 0 ; j < n ; j++) {
                scanf("%d" , &a[i][j]);
                dp[0][i][j] = -inf;
            }
        }
        dp[0][0][0] = a[0][0];
        for(int step = 1 ; step <= 2 * n - 2 ; step++) {
            for(int i = 0 ; i < n ; i++) {
                for(int j = i ; j < n ; j++) {
                    dp[step][i][j] = -inf;
                    if(!Is(step , i , j)) continue;
                    if(i != j) {
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i - 1 ,j - 1));
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i - 1 , j));
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i , j - 1));
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i , j));
                        dp[step][i][j] += a[i][step - i] + a[j][step - j];
                    }
                    else {
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i - 1 , j - 1));
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i - 1 , j));
                        dp[step][i][j] = max(dp[step][i][j] , get_val(step - 1 , i , j));
                        dp[step][i][j] += a[i][step - i];
                        //这里将他们到达同一点时val就取一次那么最大值肯定不是去走同一点的。
                    }
                    //这里的转移至要注意第一个人一定在第二个人上面就行,也就是说i>=j是必须的转移时也要注意
                }
            }
        }
        printf("%d
    " , dp[2 * n - 2][n - 1][n - 1] + a[0][0] + a[n - 1][n - 1]);
        return 0;
    }
  • 相关阅读:
    JMeter基础篇--录制web脚本(四)
    jmeter的基础使用(二)
    jmeter安装教程(一)
    delete用法(删除表内容)
    update用法(修改表内容)
    IPy对网段的处理
    netmiko
    读写conf文件
    读写json文件
    excel及数据处理
  • 原文地址:https://www.cnblogs.com/TnT2333333/p/7750054.html
Copyright © 2020-2023  润新知