Relay Race (DP)
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 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
1
5
5
2
11 14
16 12
53
3
25 16 25
12 18 19
11 13 8
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.
题目大意:
输入一个n,然后输入一个n*n的矩阵,求两条从左上角到右下角的路所经过的所有的格子里的权值和,求最大和。
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[][][];///总步数,第一个人向右的步数,第二个人向右的步数
int a[][],n;
int main()
{
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
int st=*(n-);
for(int i=;i<=st;i++)
for(int j=;j<=n;j++)
for(int k=;k<=n;k++)
dp[i][j][k]=-INF;
dp[][][]=;
for(int i=;i<st;i++)
for(int j=;j<=i&&j<n;j++)
for(int k=;k<=i&&k<n;k++)
{
int x1=i-j+,y1=j+,x2=i-k+,y2=k+;///x1,y1,x2,y2代表第一个人的位置和第二个人的位置
if(x1+<=n&&x2+<=n)///下下
{
int ans;
if(x1+==x2+&&y1==y2)///相同格子
ans=a[x1+][y1];
else
ans=a[x1+][y1]+a[x2+][y2];
dp[i+][j][k]=max(dp[i+][j][k],dp[i][j][k]+ans);
}
if(x1+<=n&&y2+<=n)///下右
{
int ans;
if(x1+==x2&&y1==y2+)
ans=a[x1+][y1];
else
ans=a[x1+][y1]+a[x2][y2+];
dp[i+][j][k+]=max(dp[i+][j][k+],dp[i][j][k]+ans);
}
if(y1+<=n&&x2+<=n)///右下
{
int ans;
if(x1==x2+&&y1+==y2)
ans=a[x1][y1+];
else
ans=a[x1][y1+]+a[x2+][y2];
dp[i+][j+][k]=max(dp[i+][j+][k],dp[i][j][k]+ans);
}
if(y1+<=n&&y2+<=n)///右右
{
int ans;
if(x1==x2&&y1+==y2+)
ans=a[x1][y1+];
else
ans=a[x1][y1+]+a[x2][y2+];
dp[i+][j+][k+]=max(dp[i+][j+][k+],dp[i][j][k]+ans);
}
}
cout<<dp[st][n-][n-]+a[][]<<'\n';
return ;
}
Relay Race (DP)的更多相关文章
- LightOJ1326 Race(DP)
题目问N匹马比赛有多少种结果.一开始想用排列组合搞搞,然后发现想错了.艰难地把思路转向DP,最后AC了. dp[i][j]表示前i匹马确定出j个名次的方案数 dp[1][1]=1 对于第i匹马,它要确 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
随机推荐
- IE和火狐下的iframe刷新
前面使用了前端上传插件plupload,在谷歌浏览器中运行完全没问题,但是在IE和火狐下就出现当文档加载完成之后,该文件上传的iframe插件的上传按钮点击无效,当对上传的文件进行一次删除之后,按钮就 ...
- LightOJ 1422 Halloween Costumes (区间DP,经典)
题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...
- ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...
- Python 元组、字典、集合操作总结
元组 a=('a',) a=('a','b') 特点 有序 不可变,不可以修改元组的值,无法为元组增加或者删除元素 元组的创建 a=('a',) a=('a','b') tuple('abcd') 转 ...
- ucosii(2.89)mutex 应用要点
mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...
- 将 PROTOCOL 的方法声明为 MUTATING
将 PROTOCOL 的方法声明为 MUTATING 由 王巍 (@ONEVCAT) 发布于 2014/08/17 Swift 的 protocol 不仅可以被 class 类型实现,也适用于 str ...
- awk纯干货
AWK的惊人表现: Awk设计的目的:简化一般文本处理的工作. 属于POSIX的一部分. AWK命令行: Awk的调用可以定义变量.提供程序并且指定输入文件: Awk [ -F fs ] [ -v ...
- mongodb详细教程
转自:https://www.cnblogs.com/liruihuan/tag/MongoDB/
- Windows平台下使用vs code搭建python3环境(1)
前言:最近几周在使用python开发的过程中,碰到了好多坑,由于以前使用visual studio 2015习惯了,导致刚开始搭建python开发环境以及管理各种包的时候有点不习惯,再加上python ...
- APIO2019&&THUSC2019游记
APIO2019懵十三记: day0: 早上和ljx从沈阳出发,下午一点到的首师大附. 由于工作人员中午十二点就散了,我们就先去试机了. 下午三点接到狗牌和T恤,晚上买麦当劳回如意吃. 晚上还有场模拟 ...