数字三角形模型

摘花生

acwing1015.摘花生
问题描述

数字三角形模型_第1张图片
题解
数字三角形模型_第2张图片

#include
#include

using namespace std;

const int N = 110;
int raw,col;  //前一个是行,后一个是列
int w[N][N];
int f[N][N];

int dp(){
    for(int i = 1;i <= raw;i++)
        for(int j = 1;j <= col;j++){
            f[i][j] = max(f[i - 1][j],f[i][j - 1]) + w[i][j];
        }
}

int main(){

    int t;
    cin >> t;
    while(t--){
        cin >> raw >> col;
        for(int i = 1;i <= raw;i++){
            for(int j = 1;j <= col;j++){
                cin >> w[i][j];
            }
        }
        dp();
        cout << f[raw][col] << endl;
    }

    return 0;
}

最低通行费

acwing1018.最低通行费

题解
数字三角形模型_第3张图片
需要考虑边界问题
数字三角形模型_第4张图片

#include
#include

using namespace std;

const int N = 110,INF = 1e9;
int n;
int w[N][N];
int f[N][N];

int main(){

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio();
    cin >> n;
    for(int i = 1;i <= n;i++) {
        for (int j = 1; j <= n; j++) {
            cin >> w[i][j];
        }
    }

    for(int i = 1;i <= n;i++) {
        for (int j = 1; j <= n; j++) {
            if(i == 1 && j == 1)
                f[i][j] = w[i][j];
            else{
                f[i][j] = INF;
                if(i > 1)
                    f[i][j] = min(f[i][j],f[i - 1][j] + w[i][j]);
                if(j > 1)
                    f[i][j] = min(f[i][j],f[i][j - 1] + w[i][j]);
            }
        }
    }

    cout << f[n][n] << endl;

    return 0;
}

方格取数

acwing1027.方格取数
题解
数字三角形模型_第5张图片

#include
#include

using namespace std;

const int N = 15;

int n;
int w[N][N];
int f[N * 2][N][N];

int main(){

    cin >> n;
    int a,b,c;
    while(cin >> a >> b >> c,a || b || c)
        w[a][b] = c;

    for(int k = 2;k <= n + n;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 t = w[i1][j1];
                    if(i1 != i2)
                        t += w[i2][j2];
                    f[k][i1][i2] = max(f[k][i1][i2],f[k - 1][i1 - 1][i2 - 1] + t);
                    f[k][i1][i2] = max(f[k][i1][i2],f[k - 1][i1 - 1][i2] + t);
                    f[k][i1][i2] = max(f[k][i1][i2],f[k - 1][i1][i2 - 1] + t);
                    f[k][i1][i2] = max(f[k][i1][i2],f[k - 1][i1][i2] + t);
                }
            }
        }
    }

    cout << f[n + n][n][n] << endl;

    return 0;
}

传纸条

acwing175.传纸条

#include 
#include 
#include 
#include 

using namespace std;

const int N = 55;

int n, m;
int g[N][N];
int f[N * 2][N][N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            scanf("%d", &g[i][j]);

    for (int k = 2; k <= n + m; k ++ )
        for (int i = max(1, k - m); i <= n && i < k; i ++ )
            for (int j = max(1, k - m); j <= n && j < k; j ++ )
                for (int a = 0; a <= 1; a ++ )
                    for (int b = 0; b <= 1; b ++ )
                    {
                        int t = g[i][k - i];
                        if (i != j || k == 2 || k == n + m)
                        {
                            t += g[j][k - j];
                            f[k][i][j] = max(f[k][i][j], f[k - 1][i - a][j - b] + t);
                        }
                    }

    printf("%d\n", f[n + m][n][n]);

    return 0;
}

你可能感兴趣的:(ACM理论)