xiaojie4620

导航

NOIP2000提高组 方格取数

第一种解法

四维暴力DP

Code

```

 

 1 #include <iostream>
 2 
 3 using namespace std;
 4 const int N = 22;
 5 
 6 int a[N][N];
 7 int res;
 8 int n;
 9 int x,y,k;
10 int f[N][N][N][N];
11 
12 int main()
13 {
14     cin >> n;
15     while( cin >> x >> y >> k , x || y || k)
16      a[x][y] = k;
17     
18     for (int i = 1; i <= n; i++)
19         for (int j = 1; j <= n; j++)
20             for (int k = 1; k <= n; k++)
21                 for (int l = 1; l <= n; l++)
22                 {
23                     f[i][j][k][l] = max(f[i - 1][j][k - 1][l] , max(f[i - 1][j][k][l - 1], max(f[i][j - 1][k - 1][l], f[i][j - 1][k][l - 1]))) + a[i][j] + a[k][l];
24                     if( i == k && j == l ) f[i][j][k][l] -= a[i][j];
25                 }
26     cout << f[n][n][n][n];               
27     return 0;
28 }

 

解法二

取中间一点 三维Dp

Code

```

#include <iostream>

using namespace std;

const int N = 22;

int f[N][N][N];

int n;
int x, y, c;
int a[N][N];

int main()
{
    cin >> n;
    while( cin >> x >> y >> c, x || y || c )   a[x][y] = c;
    
    for (int k = 2; k <= n << 1; k++)
        for (int i1 = 1; i1 <= n; i1++)
            for (int i2 = 1; i2 <= n; i2++)
                {
                    int j1 = k - i1, j2 = k - i2;
                    if(j1 >= 1 && j1 <= n && j2 >= 1 && j2 <= n)
                    {
                        int &w = f[k][i1][i2];
                        int t = a[i1][j1];
                        if( i1 != i2 )  t += a[i2][j2];
                        
                        w = max(w, f[k - 1][i1 - 1][i2 - 1] + t);
                        w = max(w, f[k - 1][i1 - 1][i2] + t);
                        w = max(w, f[k - 1][i1][i2 - 1] + t);
                        w = max(w, f[k - 1][i1][i2] + t);
                    }
                }
    cout << f[n << 1][n][n] << endl;
    return 0;
}

AC!!!

 

posted on 2022-03-27 15:15  kuokuo  阅读(62)  评论(0编辑  收藏  举报