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

     
     
    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
     

    Author
    yifenfei
     

    Source
     
    题目大意:从(1,1)走到(N,N),每次只能向下或者向右走,然后在走回(1,1)每次只能向上或者向左走。然后每个点上都有一个值,问你途径所能获得的值最大是多少,并且每个点只能走一次。
     
    虽然数据范围比较小可以用四维的, 但写这题是为了练习,写了个可以数据范围再稍大点的
     
    让两个进程同时进行,枚举步数 k, 当x1==x2 || y1==y2时跳过,得状态转移方程:
    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,y2-1),dp(k-1,x1,y1-1,x2,y2-1))+a(x1,y1)+a(x2,y2);
    由于只能走右或下,所以坐标满足x+y=k,这样就能降低维数为3维(y1=k-x1,y2=k-x2),方程:
    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)) + a(x1,k-x1)+a(x2,k-x2);
     
     
     
    
    
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    
    #define N 110
    const LL INF = 1e14;
    #define met(a,b) (memset(a,b,sizeof(a)))
    #define max4(a,b,c,d) (max(max(a,b),max(c,d)))
    
    
    LL a[N][N], dp[N*2][N][N];
    
    int main()
    {
        int n;
    
        while(scanf("%d", &n)!=EOF)
        {
            int i, j, k;
    
            met(a, 0);
            for(k=0; k<=n*2-2; k++)
            for(i=0; i<=n; i++)
            for(j=0; j<=n; j++)
                dp[k][i][j] = -INF;
    
            for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                scanf("%I64d", &a[i][j]);
    
            dp[0][1][1] = a[1][1];
            for(k=1; k<=n+n-2; k++)
            {
                for(i=1; i<=n; i++) ///i 代表第一个人所在的行
                for(j=1; j<=n; j++) ///j 代表第二个人所在的行
                {
                     dp[k][i][j] = max4(dp[k-1][i][j], dp[k-1][i][j-1], dp[k-1][i-1][j], dp[k-1][i-1][j-1]);
                    if(i!=j)
                        dp[k][i][j] += a[i][k+2-i] + a[j][k+2-j];
                    else
                        dp[k][i][j] += a[i][k+2-i];
                }
            }
    
            printf("%I64d
    ", dp[n+n-2][n][n]);
        }
        return 0;
    }
  • 相关阅读:
    git常用命令学习(转)
    论docker中 CMD 与 ENTRYPOINT 的区别(转)
    常见算法:C语言求最小公倍数和最大公约数三种算法
    iPhone开发【一】从HelloWorld開始
    网页代码优化
    北京簋街 美食全然攻略 + 簋街好吃的夜宵去处-----店铺介绍大全
    strtok和strtok_r
    Swift 编程语言新手教程
    java中获取系统属性以及环境变量
    读《自由人》
  • 原文地址:https://www.cnblogs.com/YY56/p/5488365.html
Copyright © 2011-2022 走看看