zoukankan      html  css  js  c++  java
  • Matrix(多线程dp)

    Matrix

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2153    Accepted Submission(s): 1135

    Problem Description
    Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix. Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end. 
     
    Input
    The input contains multiple test cases. Each case first line given the integer n (2<n<30)  Than n lines,each line include n positive integers.(<100)
     
    Output
    For each test case output the maximal values yifenfei can get.
     
    Sample Input
    2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
     
    Sample Output
    28 46 80

    题解:多线程dp;

    由于从左上到右下再回到左上,可以看成两条线从左上到右下;当x1==x2的时候跳过去;得到:

    dp(k, x1, y1, x2, y2) = max(dp(k-1, x1-1, y1, x2-1, y2), dp(k-1, x1-1, y1, x2, y2-1), dp(k-1, x1, y1-1, x2-1, y2), dp(k-1, x1, y1-1,x2, y2-1))

    又因为,步数k等于x+y,所以五维化成三维;

    得到:

    dp(k, x1, x2) = max(dp(k-1, x1, x2), dp(k-1, x1-1, x2), dp(k-1, x1, x2-1), dp(k-1, x1-1, x2-1)) + mp(x1, k-x1) + mp(x2, k-x2) ;

    所以得到代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define SD(x,y) scanf("%lf%lf",&x,&y)
    #define P_ printf(" ")
    typedef long long LL;
    int mp[50][50],dp[1010][31][31];
    int N;
    int main(){
        while(~SI(N)){
            for(int i=1;i<=N;i++)
                for(int j=1;j<=N;j++)
                    SI(mp[i][j]);
            mem(dp,0);
            for(int k=3;k<2*N;k++){
                for(int x1=1;x1<=N;x1++){
                    for(int x2=1;x2<=N;x2++){
                        if(k-x1>N||k-x2>N)continue;
                        if(x1==x2)continue;
                        dp[k][x1][x2]=max(max(dp[k-1][x1-1][x2],dp[k-1][x1][x2-1]),max(dp[k-1][x1-1][x2-1],dp[k-1][x1][x2]))+mp[x1][k-x1]+mp[x2][k-x2];
                    }
                }
            }
            printf("%d
    ",max(dp[2*N-1][N][N-1],dp[2*N-3][N-1][N])+mp[N][N]+mp[1][1]);
        }
        return 0;
    }
  • 相关阅读:
    团队冲刺(九)
    团队冲刺(八)
    团队冲刺(七)
    团队冲刺(六)
    团队冲刺(五)
    背景图片-密度屏幕(移动端)
    响应字体大小(移动端)
    FLEX 布局
    图片垂直居中
    css reset
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5204497.html
Copyright © 2011-2022 走看看