首页 > 其他 > 详细

codeforces 214E-Relay Race

时间:2014-03-13 23:17:33      阅读:828      评论:0      收藏:0      [点我收藏+]

Description

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 inton?×?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.

Sample Input

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


思路:题目的大致意思是两个人参加Relay Race,首先一个人从(1,1)出发去(n,n),走到(n,n)后另外一个人从(n,n)走到(1,1),然后求两个人沿途经过的每个格子的数最大和,重复走的格子按一次计算。

  明白这道题的意思之后,就想到这是一道dp的题,如果只是单方向的走的话,那么就会很简单了,然而是双方向,这样就要用到4维的dp,记录两个人走到的位置,然而n最大达到300,那么这样就会爆空间,看了一下别人的思路,使用3维代替4维,用dp[i][j][k](i代表走的步数,j代表第一个人所处的行,k代表第二个人所处的行,i-j代表第一个人所处的列,i-k代表第二个人所处的列),那么两个人的位置就确定了,而且节省了很多空间。

提醒:千万不要忘记dp数组的初始化,由于数据中会出现负数,不初始化就会出错。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int a[305][305],dp[610][305][305];
int n;
void chu()
{
    for(int i=1;i<=n+n-1;i++)
	for(int j=0;j<=n;j++)
	for(int k=0;k<=n;k++)
		dp[i][j][k]=-1000000;

}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        chu();
        for(int i=0;i<=n;i++)
            a[0][i]=-10000;
        for(int i=1;i<=n;i++)
        {
            a[i][0]=-10000;
            for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
        }

         if(n==1)
        {
            printf("%d\n",a[1][1]);
            continue;
        }
     int i,j,k;
     dp[1][1][1]=a[1][1];
     for(i=2;i<=n+n-1;i++)
     {
         for(j=1;j<=n;j++)
         {
             if((i-j+1>=1)&&(i-j+1<=n))
             for(k=1;k<=n;k++)
             {
                 if((i-k+1>=1)&&(i-k+1<=n))
                 {
                 if(j!=k)
                 dp[i][j][k]=max(max(dp[i-1][j][k],dp[i-1][j-1][k]),max(dp[i-1][j][k-1],dp[i-1][j-1][k-1]))+a[j][i-j+1]+a[k][i-k+1];
                 else dp[i][j][k]=max(max(dp[i-1][j][k],dp[i-1][j-1][k]),max(dp[i-1][j][k-1],dp[i-1][j-1][k-1]))+a[j][i-j+1];
                 //printf("i=%d j=%d k=%d dp=%d\n",i,j,k,dp[i][j][k]);
                 }
             }
         }
     }
      printf("%d\n",dp[n+n-1][n][n]);
    }
    return 0;
}



codeforces 214E-Relay Race,布布扣,bubuko.com

codeforces 214E-Relay Race

原文:http://blog.csdn.net/knight_kaka/article/details/21184961

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!