Codeforces 214E. Relay Race dp
来源:互联网 发布:驱动软件下载排行榜 编辑:程序博客网 时间:2024/06/06 00:51
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.
15
5
211 1416 12
53
325 16 2512 18 1911 13 8
136
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:
题意:找从1,1点到n,n点的两条路径,使路径上的值最大。其中重复点只能算一次。
思路:肯定是dp,但状态不好表示,用dp[i][j][k]表示走了i步,一条路径在第j行,另一条在第k行时的最大值,则用三维数组就能将两条路径的横纵坐标都表示出来,就可以做了。
状态方程:dp[i][j][k]=max(dp[i-1][j-1][k-1],dp[i-1][j][k-1],dp[i-1][j-1][k],dp[i-1][j][k])+c;c为该状态下能加上的值(两点或一点)。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>using namespace std;const int N=305;const int inf=0x3f3f3f3f;const double eps=1e-5;int n,c;int dp[605][N][N],a[N][N];int max(int a,int b,int c,int d){ return max(max(a,b),max(c,d));}int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); memset(dp,-inf,sizeof(dp)); dp[0][1][1]=a[1][1]; int l=(n-1)*2; for(int i=1;i<=l;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ if(k==j)c=a[j][i+2-j]; else c=a[j][i+2-j]+a[k][i+2-k]; dp[i][j][k]=max(dp[i-1][j-1][k-1],dp[i-1][j][k-1],dp[i-1][j-1][k],dp[i-1][j][k])+c; } } } printf("%d\n",dp[l][n][n]); return 0;}
- Codeforces 214E. Relay Race dp
- Codeforces 214E Relay Race【Dp】
- codeforces 214E-Relay Race
- CodeForces 214E - Relay Race(二取方格数)
- codeforces 213C Relay Race(dp)
- CodeForces 213C Relay Race(dp)
- Codeforces Round #131 (Div. 2) E. Relay Race
- codeforces 213C Relay Race(DP基础,棋盘路径)
- CodeForces 213C Relay Race
- CodeForces 213C-Relay Race
- Codeforces 213C Relay Race
- codeforces 43E Race
- CodeForces213C.Relay Race
- 【dp】codeforces 83E
- codeforces-731E-dp
- Codeforces 748E dp
- Codeforces 722E [DP]
- Codeforces 416E DP
- java中的final变量的用法
- 工作心得(二)
- 【二分答案+枚举】AOJ-215 Mountain Walking
- 深入解析java乱码
- linux内核相关镜像uImage、zImage、bzImage、vmlinuz、vmlinux、Image.rom 、Image.ram 关系分析
- Codeforces 214E. Relay Race dp
- CRC校验的原理和 CRC的用途。
- Xcode5 上64位编译 出错No architectures to compile for
- Accelerated C++:通过示例进行编程实践——练习解答(第1章)
- 异步FIFO的Verilog代码 DCFIFO实现
- Json与Java对象互转之Gson学习
- Git使用基础
- HTML5 经典弹球过关游戏的简化版本
- linux/unix下setuid/seteuid/setreuid/setresuid