首页 > 代码库 > Codeforces Round #245 (Div. 1)——Working out

Codeforces Round #245 (Div. 1)——Working out

题目链接

  • 题意:
    一个n*m的矩阵,每个方格有一个非负数,现在选择两条线路:一个左上到右下,一个左下到右上,且只能有一个公共点。求两个线路上数的最大值(公共点不算)
  • 分析:
    只有两种情况,dp即可。记两个线路为1和2,考虑一个公共点,1为左进右出,2为下进上出;1上进下出,2为左进右出
const int MAXN = 1005;

int lu[MAXN][MAXN], ld[MAXN][MAXN];
int ru[MAXN][MAXN], rd[MAXN][MAXN];
int ipt[MAXN][MAXN];
int n, m;

int main()
{
//    freopen("in.txt", "r", stdin);
    while (~RII(n, m))
    {
        CLR(ipt, -1);
        FE(i, 1, n) FE(j, 1, m) RI(ipt[i][j]);
        lu[1][1] = ipt[1][1];
        ru[1][m] = ipt[1][m];
        ld[n][1] = ipt[n][1];
        rd[n][m] = ipt[n][m];

        FE(i, 1, n) FE(j, 1, m)
        {
            lu[i][j + 1] = max(lu[i][j + 1], lu[i][j] + ipt[i][j + 1]);
			lu[i + 1][j] = max(lu[i + 1][j], lu[i][j] + ipt[i + 1][j]);
        }
        FE(i, 1, n) FED(j, m, 1)
        {
            ru[i][j - 1] = max(ru[i][j - 1], ru[i][j] + ipt[i][j - 1]);
			ru[i + 1][j] = max(ru[i + 1][j], ru[i][j] + ipt[i + 1][j]);
        }
        FED(i, n, 1) FE(j, 1, m)
        {
            ld[i][j + 1] = max(ld[i][j + 1], ld[i][j] + ipt[i][j + 1]);
			ld[i - 1][j] = max(ld[i - 1][j], ld[i][j] + ipt[i - 1][j]);
        }
        FED(i, n, 1) FED(j, m, 1)
        {
            rd[i][j - 1] = max(rd[i][j - 1], rd[i][j] + ipt[i][j - 1]);
			rd[i - 1][j] = max(rd[i - 1][j], rd[i][j] + ipt[i - 1][j]);
        }
        int ans = 0;
        FE(i, 1, n) FE(j, 1, m)
        {
            if (i - 1 >= 1 && j - 1 >= 1 && i + 1 <= n && j + 1 <= m)
            {
                ans = max(ans, lu[i - 1][j] + ld[i][j - 1] + rd[i + 1][j] + ru[i][j + 1]);
            }
            if (j - 1 >= 1 && i + 1 <= n && j + 1 <= m && i - 1 >= 1)
            {
                ans = max(ans, lu[i][j - 1] + ld[i + 1][j] + rd[i][j + 1] + ru[i - 1][j]);
            }
        }
        WI(ans);
    }
    return 0;
}