Codeforces 214E Relay Race【Dp】

本文介绍了一个接力赛场景下的路径选择问题,目标是通过合理的路径规划使两名选手在规定的范围内移动并获得最大积分。文章详细解释了解决该问题的动态规划方法,并提供了实现代码。

E. Relay Race
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
1
5
output
5
input
2
11 14
16 12
output
53
input
3
25 16 25
12 18 19
11 13 8
output
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.


题目大意:

从左上角只能向右向下走,有两个人一起走,求走过的格子的价值和,一个格子走过多次只能积累一次和。

问最大能够积累多大的和。


思路:


因为存在负权值,所以这个问题我们不能考虑用费用流去做。


我们考虑Dp。最简单最暴力的方法就是去设定Dp【i】【j】【x】【y】表示第一个人走到了(i,j)这个点,另一个人走到了(x,y)这个点能够获得的最优价值和。

但是考虑n比较大,O(n^4)去做不仅空间炸了,时间也炸了。


那么考虑设定Dp【i】【x】【step】表示第一个人走到了第i行,第二个人走到了第x行,一共走了step步的最优价值和,那么有:

当前step步,第一个人位子为:i,2+step-i,第二个人的位子为:x,2+step-x;

那么对于两个人,一共四种走法,转移一下有:



过程维护一下即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[350][350];
int dp[305][305][305*2+5];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=0;k<=2*n;k++)
                {
                    dp[i][j][k]=-0x3f3f3f3f;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        dp[1][1][0]=a[1][1];
        for(int i=1;i<=n;i++)
        {
            for(int x=1;x<=n;x++)
            {
                for(int step=1;step<=2*n-1;step++)
                {
                    int j=2+step-i;
                    int y=2+step-x;
                    if(i==1&&j==1)continue;
                    if(x==1&&y==1)continue;
                    if(j>=1&&j<=n&&y>=1&&y<=n)
                    {
                        dp[i][x][step]=max(dp[i][x][step],dp[i-1][x-1][step-1]+a[i][j]+a[x][y]);
                        dp[i][x][step]=max(dp[i][x][step],dp[i-1][x][step-1]+a[i][j]+a[x][y]);
                        dp[i][x][step]=max(dp[i][x][step],dp[i][x-1][step-1]+a[i][j]+a[x][y]);
                        dp[i][x][step]=max(dp[i][x][step],dp[i][x][step-1]+a[i][j]+a[x][y]);
                        if(i==x&&j==y)dp[i][x][step]-=a[x][y];
                    }
                }
            }
        }
        printf("%d\n",dp[n][n][2*n-2]);
    }
}











### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值