Relay Race CodeForces - 214E [DP]

E. Relay Race
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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, j), then he can move to cell (i - 1, j) or (i, 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 n integers 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)(1, 2)(2, 2), and for Rubik: (2, 2)(2, 1)(1, 1).

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

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

题意:两个人均从(1,1)出发,要走到(n,n) .  如果走到重复的格子,只算一次.问最大和是多少

思路:
p[i][x][step]表示第一个人在第i行,第二个人在第x行,每个人共走step步数所得到的最大值那么有

dp[i][x][step]=max(dp[i][x][step],dp[i-1][x-1][step-1]+a[i][j]+a[x][y]);
dp[i][x][step]=max(dp[i][x][step],dp[i][x-1][step-1]+a[i][j]+a[x][y]);
dp[i][x][step]=max(dp[i][x][step],dp[i-1][x][step-1]+a[i][j]+a[x][y]);
dp[i][x][step]=max(dp[i][x][step],dp[i][x][step-1]+a[i][j]+a[x][y]);

if(i==x && j==y) dp[i][x][step]-=a[i][j];
答案即为 dp[n][n][2*n-2];
复杂度为O(n*n*(2n)) = 5.4*10^7

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define bug1 cout <<"bug1"<<endl
#define bug2 cout <<"bug2"<<endl
#define bug3 cout <<"bug3"<<endl
using namespace std;
typedef long long ll;

const int MAX_N=300+5;

int a[MAX_N][MAX_N];
int dp[MAX_N][MAX_N][2*MAX_N+50];

int main(void){
    int n;
    cin >>n ;
    for(register int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)   scanf("%d",&a[i][j]);
    for(register int i=0;i<=n;i++)
        for(register int j=0;j<=n;j++)
            for(register int step=0;step<=2*n;step++)    dp[i][j][step]=-INF;
    dp[1][1][0]=a[1][1];
    for(register int i=1;i<=n;i++){
        for(register int x=1;x<=n;x++){
            for(register int step=0;step<=2*n-1;step++){
                int j=step-i+2;
                int y=step-x+2;
//                if(i==1 &&j==1) continue;
//                if(x==1 && y==1) continue;
                if(j>=1&&j<=n&&y>=1&&y<=n ){
                    dp[i][x][step]=max(dp[i][x][step],dp[i-1][x-1][step-1]+a[i][j]+a[x][y]);
                    dp[i][x][step]=max(dp[i][x][step],dp[i][x-1][step-1]+a[i][j]+a[x][y]);
                    dp[i][x][step]=max(dp[i][x][step],dp[i-1][x][step-1]+a[i][j]+a[x][y]);
                    dp[i][x][step]=max(dp[i][x][step],dp[i][x][step-1]+a[i][j]+a[x][y]);
                    if(i==x && j==y) dp[i][x][step]-=a[i][j];
                    cout << "i="<<i<<"j="<<j<<" "<<"step="<<step<<" "<<dp[1][1][0] << endl;
                }
            }
        }
    }
    //cout <<"******"<<endl;
    cout <<dp[n][n][2*n-2] << endl;
}


全部评论

相关推荐

点赞 评论 收藏
分享
07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务