CodeForces 762D Maximum path
http://codeforces.com/problemset/problem/762/D
因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步
并走完一列来替换 那么走的方法就大大减少 左边一列转移到右边一列 每个
格子的转移方法枚举出来 用动态规划即可解决
最主要的是因为他能够往回走.
但是我们画图可以发现:每次往回走一定不用超过1次.
也就是说,最多只能走成这样
而不会走成这样
因为下图的走法一定可以用上图组合,并且
由于只用3行的特性,每次向回走实际上是取走了所有的数.
所以我们只采用上图方式得出来的答案一定最优
#include <bits/stdc++.h>
#define INF 0x7fffffff
using namespace std; typedef long long LL;
LL grid[][];
LL tmp[][];
LL dp[][];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < ; i++)
for (int j = ; j < n; j++)
{
scanf("%lld", &grid[i][j]);
}
dp[][] = grid[][];
dp[][] = grid[][] + grid[][];
dp[][] = grid[][] + grid[][] + grid[][];
tmp[][] = grid[][];
tmp[][] = grid[][];
tmp[][] = grid[][];
for(int j = ;j < n; j++)
{
for (int i = ; i < ; i++)
{
dp[i][j] = tmp[i][j] = dp[i][j-] + grid[i][j];
}//这样的转移走法 包括了所有的走法
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j-] + grid[][j] + grid[][j] + grid[][j-] + grid[][j-] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j-] + grid[][j] + grid[][j] + grid[][j-] + grid[][j-] + grid[][j]);
}
cout << dp[][n-] << endl;
return ;
}
dp[i][j] i 行 j 列可以得到的最大值
tmp[i][j]直接从右边一个走过来的得到的值
CodeForces 762D Maximum path的更多相关文章
- Codeforces 762D Maximum path 动态规划
Codeforces 762D 题目大意: 给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍.经过一个格子会获得格子中的权值.每个格子的权值\(a ...
- cf 762D. Maximum path
天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选 ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- LeetCode124:Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
随机推荐
- ubuntu 16.0 利用ant编译 hadoop-eclipse-plugins2.6.0
折腾了两天,抱着不放弃的精神,我终于编译出我自己所需的hadoop中在eclipse中的插件 在网上下载的可能因为版本不一致,在编译的时候出现各种各样的问题,包括你的eclipse版本和hadoop版 ...
- javascript基本类型和引用类型,作用域和内存问题
基本类型(null.undefined.boolean.number.string)和引用类型(Object 对象) 1 基本类型:只能不存一个值,一种类型:从一个变量向另一个变量复制基本类型的值, ...
- 如何启动Intel VT-x
如何启动Intel VT-x 5 在64bit win7系统下安装了Vmware10,然后安装64位的UbuntuKylin 14.04,想要打开UbuntuKylin,弹出如下对话框: 请问该如何启 ...
- kafka 安装以及测试
1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper ...
- idea Please specify commit message
在idea中使用github来进行版本控制的时候, 当点击提交的时候遇到了这个问题 错误: Please specify commit message 解决方法: 在commit message中填写 ...
- 【414】Code::Blocks增加主题
替换文件地址:C:\Users\z5194293\AppData\Roaming\CodeBlocks 文件下载地址:default.rar 通过 Settings -> Editor... - ...
- JS中鼠标左右键以及中键的事件
在三维场景中有时候需要判断鼠标的事件,除了使用的click事件,只有鼠标左键有效,而右键无效.而对于onmousedown.onmouseup的时候鼠标的事件左键/右键有效.详细请看w3c上的资料. ...
- velocity生成静态页面代码
首先需要必备的jar包: web.xml <!-- load velocity property --> <servlet> <servlet-name>veloc ...
- EXTJS中文乱码
在<head>中加入 <meta http-equiv="Content-Type" content="text/html; charset=GB231 ...
- shell脚本,文件里面的英文大小写替换方法。
[root@localhost wyb]# cat daxiaoxie qweBNMacb eeeDFSmkl svdIOPtyu [root@localhost wyb]# cat daxiaoxi ...