CodeForces 213C Relay Race
来源:互联网 发布:化工设备画图软件 编辑:程序博客网 时间:2024/06/06 03:19
Description
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 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).
Output
On a single line print a single number — the answer to the problem.
Sample Input
15
5
211 1416 12
53
325 16 2512 18 1911 13 8
136
Hint
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.
走两条路,要同时处理呢。
#include<iostream> #include<algorithm>#include<math.h>#include<cstdio>#include<string>#include<string.h>using namespace std;const int maxn = 305;int n, a[maxn][maxn], f[maxn][maxn][maxn], i, j, k;int main(){while (~scanf("%d", &n)){memset(f, 0, sizeof(f));for (i = 1; i <= n; i++)for (j = 1; j <= n; j++) scanf("%d", &a[i][j]);for (i = 0; i <= n; i++)for (j = 0; j <= n; j++) for (k = 0; k <= n; k++) f[i][j][k] = -10000000;f[1][1][1] = a[1][1];for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)for (k = 1; k <= n; k++)if (i + j > k&&i + j <= k + n)if (i != k){f[i][j][k] = max(max(max(f[i][j - 1][k], f[i - 1][j][k]), f[i - 1][j][k - 1]), f[i][j - 1][k - 1]);f[i][j][k] += a[i][j] + a[k][i + j - k];}else if (i == 1 & j == 1) continue;else f[i][j][k] = max(max(max(f[i][j - 1][k], f[i - 1][j][k]), f[i - 1][j][k - 1]), f[i][j - 1][k - 1]) + a[i][j];if (n == 1) printf("%d\n", a[1][1]);else printf("%d\n", f[n][n][n]);}return 0;}
- CodeForces 213C Relay Race
- CodeForces 213C-Relay Race
- Codeforces 213C Relay Race
- codeforces 213C Relay Race(dp)
- CodeForces 213C Relay Race(dp)
- codeforces 213C Relay Race(DP基础,棋盘路径)
- codeforces 214E-Relay Race
- Codeforces 214E. Relay Race dp
- Codeforces 214E Relay Race【Dp】
- Codeforces Round #131 (Div. 2) E. Relay Race
- CodeForces 214E - Relay Race(二取方格数)
- CodeForces213C.Relay Race
- Codeforces 48C The Race 模拟题
- codeforces #328 C. The Big Race
- Codeforces 592C The Big Race 【GCD && LCM】
- Codeforces Round #328 (Div. 2) C. The Big Race
- Codeforces Round #328 (Div. 2)C. The Big Race
- Codeforces Round #328 (Div. 2)C. The Big Race
- CodeForces 166E Tetrahedron
- 计算机组成原理复习笔记1
- 天涯明月刀 题六
- CodeForces 118D Caesar's Legions
- PIGS (poj 1149 最大流)
- CodeForces 213C Relay Race
- hdu 1535 Invitation Cards 大年初一首A 一次正向SPFA+一次逆向SPFA
- linux inittab文件 6种启动模式
- 欢迎使用CSDN-markdown编辑器
- python基础语法学习(一)
- Largest Rectangle in Histogram
- c/c++获取时间
- C++多态性:虚函数的调用原理
- Linux多线程实践(6) --Posix读写锁解决读者写者问题