HDU 2686 Matrix By Assassin 多线程dp

来源:互联网 发布:php redis 消息队列 编辑:程序博客网 时间:2024/05/16 10:15

Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2427 Accepted Submission(s): 1284

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

#include<iostream>#include<stdio.h>#include<string.h>#define input freopen("input.txt","r",stdin)using namespace std;long long dp[32][32][32];long long mp[32][32];int n;int inrange(long long x1,long long y1,long long x2){    if(x1==1&&y1==1&&x2==1) return 1;    if(x1==x2) return 0;    if(x1>=1&&x1<=n&&y1>=1&&y1<=n&&x2>=1&&x2<=n&&x1+y1-x2>=1&&x1+y1-x2<=n&&x1<=x2)return 1;    return 0;}int main(){    //input;    int i,j,k1,k2;    while(scanf("%d",&n)!=EOF){        memset(dp,0,sizeof(dp));        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                scanf("%lld",&mp[i][j]);            }        }        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                for(k1=1;k1<=n;k1++){                    if(i==1&&j==1&&k1==1){                        dp[1][1][1]=mp[1][1];                        continue;                    }                    else if(i==n&&j==n&&k1==n){                        dp[n][n][n]=dp[n-1][n][n]+mp[n][n];                        continue;                    }                    else{                        long long maxx=0;                        bool flag=false;                        if(inrange(i-1,j,k1-1)) flag=true,maxx=max(maxx,dp[i-1][j][k1-1]);                        if(inrange(i-1,j,k1)) flag=true,maxx=max(maxx,dp[i-1][j][k1]);                        if(inrange(i,j-1,k1-1)) flag=true,maxx=max(maxx,dp[i][j-1][k1-1]);                        if(inrange(i,j-1,k1)) flag=true,maxx=max(maxx,dp[i][j-1][k1]);                        if(inrange(i,j,k1)&&flag==true)                            dp[i][j][k1]=maxx+mp[i][j]+mp[k1][i+j-k1];                        //cout<<i<<" "<<j<<" "<<k1<<" "<<i+j-k1<<" "<<dp[i][j][k1]<<endl;                    }                }            }         }           cout<<dp[n][n][n]<<endl;    }    return 0;} 
0 0
原创粉丝点击