CodeForces 213C-Relay Race
来源:互联网 发布:linux 下安装vsftpd 编辑:程序博客网 时间:2024/05/17 09:09
题目
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side ofn 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 nextn lines contain n integers each: thej-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:
题义
给出n*n的方格,每个格子上有1个数,有1个人从(1,1)走到(n,n),只能往下或往右走,另一个从(n,n)到(1,1),只能往上或往左走,当某个人经过格点时会得到格点的数,但一个格点子能被拿一次,问两个人能得到和的最大值。
解法
把问题转换为两个人都从(1,1)走到(n,n),两个人同时走,设dp[i][j][k]表示两个人都走了i步,其中第一个人向右走了j步,第二个人向右走了k步
怎样就可以计算出两个人的当前坐标 x1=i-j+1,y1=j+1,x2=i-k+1,y2=k+1
然后往后递推,两个人分别可以往下或往右走,一共4种情况
答案是dp[2*n-2][n-1][n-1]
代码
#include<cstdio>#define INF 1000000000int dp[605][305][305];int a[305][305];int max(int a,int b){return a>b?a:b;}int main(){int n,i,j,k;scanf("%d",&n);for(i=1;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);int tol=2*(n-1);for(i=0;i<=tol;i++)for(j=0;j<=n;j++)for(k=0;k<=n;k++)dp[i][j][k]=-INF;//初值要设成负的,因为有负值dp[0][0][0]=0;for(i=0;i<tol;i++)for(j=0;j<=i&&j<=n-1;j++)for(k=0;k<=i&&k<=n-1;k++){int x1=i-j+1,y1=j+1,x2=i-k+1,y2=k+1;//当前所在位置if(y1+1<=n&&x2+1<=n)//1往右2往下{int nx1=x1,ny1=y1+1,nx2=x2+1,ny2=y2;int tmp;if(nx1==nx2&&ny1==ny2)tmp=a[nx1][ny1];elsetmp=a[nx1][ny1]+a[nx2][ny2];dp[i+1][j+1][k]=max(dp[i+1][j+1][k],dp[i][j][k]+tmp);}if(x1+1<=n&&y2+1<=n)//1往下2往右{int nx1=x1+1,ny1=y1,nx2=x2,ny2=y2+1;int tmp;if(nx1==nx2&&ny1==ny2)tmp=a[nx1][ny1];elsetmp=a[nx1][ny1]+a[nx2][ny2];dp[i+1][j][k+1]=max(dp[i+1][j][k+1],dp[i][j][k]+tmp);}if(x1+1<=n&&x2+1<=n)//1往下2往下{int nx1=x1+1,ny1=y1,nx2=x2+1,ny2=y2;int tmp;if(nx1==nx2&&ny1==ny2)tmp=a[nx1][ny1];elsetmp=a[nx1][ny1]+a[nx2][ny2];dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]+tmp);}if(y1+1<=n&&y2+1<=n)//1往右2往右{int nx1=x1,ny1=y1+1,nx2=x2,ny2=y2+1;int tmp;if(nx1==nx2&&ny1==ny2)tmp=a[nx1][ny1];elsetmp=a[nx1][ny1]+a[nx2][ny2];dp[i+1][j+1][k+1]=max(dp[i+1][j+1][k+1],dp[i][j][k]+tmp);}}printf("%d\n",dp[tol][n-1][n-1]+a[1][1]);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
- [windows 10编译CNTK]
- android bitmap compress(图片压缩)
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- android开发之Nine-Patch图片
- HDU 2546 饭卡 (变形01背包)
- CodeForces 213C-Relay Race
- [Cloud Computing]Mechanisms: Data Transport
- Android进阶之6.0运行时权限处理
- How to display a byte array as hex values
- 机器学习算法:从原理到实践(持续更新中...)
- thinkPHP学习_第一天
- The value of the project attribute is null
- xxtea 加密解密
- JAVA读取、写入、更新CLOB字段