[Review] NOIP exam topics Luogu P1006

Thinking

Seeking a maximum value, the DP is in general. The subject has shown signs of such cost flow, and construction side is obvious, but too much trouble, there is no write ...... DP program this problem is not difficult, since it is back and forth, then we can put a man split into two people, ah, so for "first person" enumeration i Row, fi,j The first to express j The best solution when the grid, for the "second person" Enumeration k Row fk,l Empathy. So we Couchu a four-dimensional DP:

fi,j,k,l=max(fi1,j,k1,l,fi,j1,k1,l,fi1,j,k,l1,fi,j1,k,l1)+ai,j+ak,l

Finally theoretically output fn,m,n,m In fact we can find in the calculations only count to fn,m1,n1,m , The output of which can be friends.

Code

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>

int nextInt()
{
    int num = 0;
    char c;
    bool flag = false;
    while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
    if (c == '-')
        flag = true;
    else
        num = c - 48;
    while (std::isdigit(c = std::getchar()))
        num = num * 10 + c - 48;
    return (flag ? -1 : 1) * num;
}

int max4(const int a, const int b, const int c, const int d)
{
    int x = std::max(a, b);
    if (c > x)
        x = c;
    if (d > x)
        x = d;
    return x;
}

const size_t _Siz = 51u;

int f[_Siz][_Siz][_Siz][_Siz] = { 0 };
int a[_Siz][_Siz] = { 0 };

int n, m;

int main(int argc, char **argv)
{
    n = nextInt();
    m = nextInt();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            a[i][j] = nextInt();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int k = 1; k <= n; k++)
                for (int l = j + 1; l <= m; l++)
                    f[i][j][k][l] = max4(f[i][j - 1][k - 1][l], f[i - 1][j][k][l - 1], f[i][j - 1][k][l - 1], f[i - 1][j][k - 1][l]) + a[i][j] + a[k][l];
    std::cout << f[n][m - 1][n - 1][m] << std::endl;
#ifdef __EDWARD_EDIT
    std::cin.get();
    std::cin.get();
#endif
    return 0;
}
Published 40 original articles · won praise 0 · Views 5152

Guess you like

Origin blog.csdn.net/edward00324258/article/details/78387657