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) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
随机推荐
- OC 思维导向图
iOS 扩展思维导向图,如下图所示:
- 转:android 屏幕适配小结
做android开发,开源嘛,满市场都是凌乱的机型,总少不了适配这样或那样的型号.在这里分享一下自己在开发中用到的方法. 首先要介绍一下drawable-mdpi.drawable-hdpi-1280 ...
- uiviewcontroller 键盘不遮挡信息
//添加监听事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow: ...
- v-if与v-show的区别与选择
v-if与v-show的区别与选择 官网给的区别 v-if 是“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if也是惰性的:如果在初始渲染时条件 ...
- Robotium实践之路源码创建测试项目
1.JDK安装及环境配置 2.Eclipse安装 3.ADT插件安装 4.模拟器安装 5.准备源码 6.引进源码置项目中 .文件 .导入 .选择现有项目置工作空间中 .浏览 .选择项目,选择模拟器版本 ...
- Alfred的配置和使用
http://www.jianshu.com/p/f77ad047f7b0 Alfred:mac上的神兵利器,提升工作效率*n,快捷键:option + 空格.鉴于是看了池老师的<人生元编程 ...
- 有C++特色的极乐净土
闲的没事瞎打的 在win7下会走调,需要将win7的beep系统文件改成xp的,且主机装有蜂鸣器才能正常收听. beep文件的度盘地址(不过应该没人为了听个这个去改系统文件)(P.S.如果想要尝试,尽 ...
- shell脚本,计算输入给定的数,判断最大值,最小值,总和?
[root@localhost ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s= read -p "please input:&quo ...
- Spring框架context的注解管理方法之二 使用注解注入基本类型和对象属性 注解annotation和配置文件混合使用(半注解)
首先还是xml的配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- ios设备屏幕尺寸与分辨率
iOS 设备的屏幕尺寸.分辨率及其屏幕边长比例详细情况是怎样的? 根据屏幕尺寸和分辨率,ios现在数起来有6个版本.一,3GS:二,4s为代表:三,iphone5:四,ipad2为代表:五,ipad4 ...