加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Relay Race (DP)

发布时间:2020-12-14 03:21:00 所属栏目:大数据 来源:网络整理
导读:Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of? n ?meters. The given square is split into? n ?×? n ?cells (represented as unit squares),each cell has some number. At the beginning of

Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of?n?meters. The given square is split into?n?×?n?cells (represented as unit squares),each cell has some number.

At the beginning of the race Furik stands in a cell with coordinates?(1,?1),and Rubik stands in a cell with coordinates?(n,?n). Right after the start Furik runs towards Rubik,besides,if Furik stands at a cell with coordinates?(i,?j),then he can move to cell?(i?+?1,?j)?or?(i,?j?+?1). After Furik reaches Rubik,Rubik starts running from cell with coordinates?(n,?n)?to cell with coordinates?(1,?1). If Rubik stands in cell?(i,then he can move to cell?(i?-?1,?j?-?1). Neither Furik,nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries,he will be disqualified.

To win the race,Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited.?Each cell counts only once in the sum.

Print the maximum number of points Furik and Rubik can earn on the relay race.

Input

The first line contains a single integer?(1?≤?n?≤?300). The next?n?lines contain?nintegers each: the?j-th number on the?i-th line?ai,?j?(?-?1000?≤?ai,?j?≤?1000)?is the number written in the cell with coordinates?(i,?j).

Output

On a single line print a single number — the answer to the problem.

Examples

Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136

Note

Comments to the second sample: The profitable path for Furik is:?(1,?(1,?2),?(2,and for Rubik:?(2,?1).

Comments to the third sample: The optimal path for Furik is:?(1,?3),?(3,and for Rubik:?(3,?1). The figure to the sample:

Furik‘s path is marked with yellow,and Rubik‘s path is marked with pink.

题目大意:

输入一个n,然后输入一个n*n的矩阵,求两条从左上角到右下角的路所经过的所有的格子里的权值和,求最大和。

?

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[605][305][305];///总步数,第一个人向右的步数,第二个人向右的步数
int a[305][305],n;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    int st=2*(n-1);
    for(int i=0;i<=st;i++)
        for(int j=0;j<=n;j++)
            for(int k=0;k<=n;k++)
                dp[i][j][k]=-INF;
    dp[0][0][0]=0;
    for(int i=0;i<st;i++)
        for(int j=0;j<=i&&j<n;j++)
            for(int k=0;k<=i&&k<n;k++)
            {
                int x1=i-j+1,y1=j+1,x2=i-k+1,y2=k+1;///x1,y1,x2,y2代表第一个人的位置和第二个人的位置
                if(x1+1<=n&&x2+1<=n)///下下
                {
                    int ans;
                    if(x1+1==x2+1&&y1==y2)///相同格子
                        ans=a[x1+1][y1];
                    else
                        ans=a[x1+1][y1]+a[x2+1][y2];
                    dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]+ans);
                }
                if(x1+1<=n&&y2+1<=n)///下右
                {
                    int ans;
                    if(x1+1==x2&&y1==y2+1)
                        ans=a[x1+1][y1];
                    else
                        ans=a[x1+1][y1]+a[x2][y2+1];
                    dp[i+1][j][k+1]=max(dp[i+1][j][k+1],dp[i][j][k]+ans);
                }
                if(y1+1<=n&&x2+1<=n)///右下
                {
                    int ans;
                    if(x1==x2+1&&y1+1==y2)
                        ans=a[x1][y1+1];
                    else
                        ans=a[x1][y1+1]+a[x2+1][y2];
                    dp[i+1][j+1][k]=max(dp[i+1][j+1][k],dp[i][j][k]+ans);
                }
                if(y1+1<=n&&y2+1<=n)///右右
                {
                    int ans;
                    if(x1==x2&&y1+1==y2+1)
                        ans=a[x1][y1+1];
                    else
                        ans=a[x1][y1+1]+a[x2][y2+1];
                    dp[i+1][j+1][k+1]=max(dp[i+1][j+1][k+1],dp[i][j][k]+ans);
                }
            }
    cout<<dp[st][n-1][n-1]+a[1][1]<<n;
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读