zoukankan      html  css  js  c++  java
  • Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接:

    http://codeforces.com/problemset/problem/214/E

    Relay Race

    time limit per test4 seconds
    memory limit per test256 megabytes
    #### 问题描述 > 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. #### 样例 > **sample input** > 3 > 25 16 25 > 12 18 19 > 11 13 8 > > **sample output** > 136

    题意

    给你一个n*n的网格,每个格子有一个数字,求从(1,1)到(n,n)的两条路线,使得经过的数字的和最大。(如果一个人经过了一个格子,下一个人再经过就只能得到0)

    题解

    看上去要开4维,其实开3维就能做的dp。
    dp[k][i1][i2]表示两个人从(0,0)点出发走到第k个从对角线上(i1+j1k&&i2+j2k)一个在(i1,k-i1),一个在(i2,k-i2)上的最大值。

    代码

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    typedef int LL;
    const int maxn = 301;
    const int INF = 0x3f3f3f3f;
    int arr[maxn][maxn];
    int n;
    
    LL dp[maxn*2][maxn][maxn];
    LL dfs(int k,int i1, int i2) {
    	if (dp[k][i1][i2]>-INF) return dp[k][i1][i2];
    	LL &res = dp[k][i1][i2];
    	int j1 = k - i1, j2 = k - i2;
    	//上上
    	if (i1 - 1 >= 0 && i2 - 1 >= 0) res = max(res, dfs(k - 1, i1 - 1, i2 - 1));
    	//上左
    	if (i1 - 1 >= 0 && j2 - 1 >= 0) res = max(res, dfs(k - 1, i1 - 1, i2));
    	//左上
    	if (j1 - 1 >= 0 && i2 - 1 >= 0) res = max(res, dfs(k - 1, i1, i2 - 1));
    	//左左
    	if (j1 - 1 >= 0 && j2 - 1 >= 0) res = max(res, dfs(k - 1, i1, i2));
    	//(i1 == i2 ? 0 : arr[i2][j2])一定要加括号!可能是res+影响的吧,不加会wa很惨orz
    	res += arr[i1][j1] + (i1 == i2 ? 0 : arr[i2][j2]);
    }
    
    int main() {
    	for (int i = 0; i < maxn * 2; i++) {
    		for (int j = 0; j < maxn; j++) {
    			for (int k = 0; k < maxn; k++) {
    				dp[i][j][k] = -INF;
    			}
    		}
    	}
    	scanf("%d", &n);
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			scanf("%d", &arr[i][j]);
    		}
    	}
    	dp[0][0][0] = arr[0][0];
    	LL ans=dfs(2*n-2,n - 1, n - 1);
    	printf("%d
    ", ans);
    	return 0;
    }
  • 相关阅读:
    Eclipse快捷键大全
    如何查看JDK_API 2019.2.23
    JXL、POI操作Excel
    Eclipse 将builder文件夹下的classes文件改路径到WEB-INF下,以及常用快捷键
    系统分盘
    U盘被识别为其他设备(显示U盘图标但是不显示盘符)的解决办法
    Oracle+jsp+Servlet的员工表的简单增删改查
    2019年3月8日09:06:02 mybatis 一对多
    linux 协议栈分享
    fib
  • 原文地址:https://www.cnblogs.com/fenice/p/5683278.html
Copyright © 2011-2022 走看看