• (多线程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;
    }
  • 相关阅读:
    JS中使用正则表达式封装的一些常用的格式验证的方法-是否外部url、是否小写、邮箱格式、是否字符、是否数组
    Java中操作字符串的工具类-判空、截取、格式化、转换驼峰、转集合和list、是否包含
    Cocos2d-x 2.0 自适应多种分辨率
    应用自定义移动设备外观
    为移动设备应用程序创建外观
    【2020-11-28】人生十三信条
    【2020-11-27】事实证明,逃避是下等策略
    Python 之web动态服务器
    Python 之pygame飞机游戏
    PHP 之转换excel表格中的经纬度
  • 原文地址:https://www.cnblogs.com/YY56/p/5488365.html
Copyright © 2020-2023  润新知