题目链接:

http://codeforces.com/problemset/problem/214/E

Relay Race

time limit per test4 seconds
memory limit per test256 megabytes
#### 问题描述
> 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.
#### 输入
> 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).
#### 输出
> On a single line print a single number — the answer to the problem.
#### 样例
> **sample input**
> 3
> 25 16 25
> 12 18 19
> 11 13 8
>
> **sample output**
> 136

题意

给你一个n*n的网格,每个格子有一个数字,求从(1,1)到(n,n)的两条路线,使得经过的数字的和最大。(如果一个人经过了一个格子,下一个人再经过就只能得到0)

题解

看上去要开4维,其实开3维就能做的dp。

dp[k][i1][i2]表示两个人从(0,0)点出发走到第k个从对角线上(i1+j1k&&i2+j2k)一个在(i1,k-i1),一个在(i2,k-i2)上的最大值。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef int LL;
const int maxn = 301;
const int INF = 0x3f3f3f3f;
int arr[maxn][maxn];
int n; LL dp[maxn*2][maxn][maxn];
LL dfs(int k,int i1, int i2) {
if (dp[k][i1][i2]>-INF) return dp[k][i1][i2];
LL &res = dp[k][i1][i2];
int j1 = k - i1, j2 = k - i2;
//上上
if (i1 - 1 >= 0 && i2 - 1 >= 0) res = max(res, dfs(k - 1, i1 - 1, i2 - 1));
//上左
if (i1 - 1 >= 0 && j2 - 1 >= 0) res = max(res, dfs(k - 1, i1 - 1, i2));
//左上
if (j1 - 1 >= 0 && i2 - 1 >= 0) res = max(res, dfs(k - 1, i1, i2 - 1));
//左左
if (j1 - 1 >= 0 && j2 - 1 >= 0) res = max(res, dfs(k - 1, i1, i2));
//(i1 == i2 ? 0 : arr[i2][j2])一定要加括号!可能是res+影响的吧,不加会wa很惨orz
res += arr[i1][j1] + (i1 == i2 ? 0 : arr[i2][j2]);
} int main() {
for (int i = 0; i < maxn * 2; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < maxn; k++) {
dp[i][j][k] = -INF;
}
}
}
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
dp[0][0][0] = arr[0][0];
LL ans=dfs(2*n-2,n - 1, n - 1);
printf("%d\n", ans);
return 0;
}

Codeforces Round #131 (Div. 2) E. Relay Race dp的更多相关文章

  1. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

  4. Codeforces Round #346 (Div. 2) D. Bicycle Race 叉积

    D. Bicycle Race 题目连接: http://www.codeforces.com/contest/659/problem/D Description Maria participates ...

  5. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  6. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  7. Codeforces Round #131 (Div. 2) : B

    首先能被2,5整除的数结尾必须是0: 如果没有0肯定不行: 然后判断他们的和ans%3: 如果==0,直接从大到小输出就行: 如果==1,要么删除它们之间最小的那个%3==1的,要么删除两个小的并且% ...

  8. Codeforces Round #131 (Div. 1) A - Game

    A. Game time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  9. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

随机推荐

  1. Winform登录、控制软件只运行一次、回车登录

    Winform登录对很多程序猿来说都有些困惑,登录进入主窗体后要销毁登录窗体,而不是隐藏哦,怎么实现呢? 先贴一段Program.cs的代码 static void Main() { Mutex mu ...

  2. 将数组之中的省份市区地区ID改成对用中文字符

    数据表数据源的省市区联动: 原始数据: //获取所有学校信息 $school=D('school'); $info=$school->getList(); 数据如下: 1 => array ...

  3. 【PHP】文件上传限制

    上传文件,只判断后缀,貌似还不是很严谨; /** * 判断文件是否合法 * @param $files * @param $arrCode * @return number|boolean */ fu ...

  4. 使用junit进行Spring测试

    这几天在做SpringMVC的项目,现在总结一下在测试的时候碰到的一些问题. 以前做项目,是在较新的MyEclipse(2013)上面进行Maven开发,pom.xml 文件是直接复制的,做测试的时候 ...

  5. delphi 资源文件详解

    delphi资源文件详解 一.引子: 现在的Windows应用程序几乎都使用图标.图片.光标.声音等,我们称它们为资源(Resource).最简单的使用资源的办法是把这些资源的源文件打入软件包,以方便 ...

  6. [下载] VS 2013 Update 4 & 社群版 (Visual Studio Community) & VS 2015 Preview预览版

    这是我的备份,原文请看http://www.dotblogs.com.tw/mis2000lab/archive/2014/11/13/vs2013_update4_community_vs2015_ ...

  7. 实战Django:官方实例Part2

    我们接着Part1部分往下讲.我们在part1中启动服务器后,并没有在管理页面中发现新添加的Polls应用,怎么办捏? 7.在管理界面中显示Question 只要注册一下这个应用就可以了.编辑poll ...

  8. SQL语句中各种数据类型转换方法总结

    1.Access 每个函数都可以强制将一个表达式转换成某种特定数据类型. 语法 CBool(expression) CByte(expression) CCur(expression) CDate(e ...

  9. ES5 vs ES6

    ES5中 var React = require('react-native'); ES6中 import React from 'react-native'; .babelrc文件中添加一下内容 { ...

  10. Android 实现子View的状态跟随父容器的状态

    最近自学着做东西,需要做一个效果,就是我ListView的Item点击下或者选中的时候,我Item里面的各个组件的状态要和我Item的状态保持一直,这样我就可以用XML,去根据组件的不同状态去实现不同 ...