1 条题解

  • 0
    @ 2021-6-14 23:29:16

    C :

    /*
    ID: Sfiction
    OJ: RQNOJ
    PROB: 314
    */
    #include <stdio.h>
    #define max(a,b) a>b?a:b
    #define min(a,b) a<b?a:b
    int main()
    {
        int map[11][11]={0},f[11][11]={0};
        int i,j,k,N,t;
        scanf("%d",&N);
        while (scanf("%d%d%d",&i,&j,&k)&&(i||j||k)) map[i][j]=k;
        for (i=2;i<=2*N;i++)
            for (t=min(i,N),j=t;j>0;j--)
                for (k=t;k>0;k--)
                {
                    f[j][k]=max(f[j][k],f[j-1][k-1]);
                    f[j][k]=max(f[j][k],f[j][k-1]);
                    f[j][k]=max(f[j][k],f[j-1][k]);
                    if (j==k) f[j][k]+=map[j][i-j];
                    else f[j][k]+=map[j][i-j]+map[k][i-k];
                }
        printf("%d",f[N][N]);
        return 0;
    }
    

    C++ :

    #include<cstdio>  
    #include<cstring>  
    #include<algorithm>  
    using namespace std;  
      
    int a[11][11];  
    int dp[21][11][11];  
    const int INF = 999999999;  
      
    int operDp(int n)  
    {  
        int i, j, k;  
        memset(dp,0,sizeof(dp));  
        for(k = 1; k <= 2 * n; k++)  
        for(i = 1; i <= k; i++)  
        for(j = 1; j <= k; j++)  
        {  
            int tmp = -INF;  
            tmp = max(tmp, dp[k-1][i-1][j-1]);  
            tmp = max(tmp, dp[k-1][i-1][j]);  
            tmp = max(tmp, dp[k-1][i][j-1]);  
            tmp = max(tmp, dp[k-1][i][j]);  
            if(i == j) dp[k][i][j] = tmp + a[k-i+1][i];  
            else dp[k][i][j] = tmp + a[k-i+1][i] + a[k-j+1][j];  
        }  
        return dp[2*n][n][n];  
    }  
      
    int main()  
    {  
        int n, r, c, v;  
        while(scanf("%d",&n) != EOF)  
        {  
            memset(a,0,sizeof(a));  
            while(true)  
            {  
                scanf("%d%d%d",&r,&c,&v);  
                if(!r && !c && !v) break;  
                a[r][c] = v;  
            }  
            printf("%d\n",operDp(n));  
        }  
        return 0;  
    }  
    

    Pascal :

    uses math;
    var
       n,x,y,s,i,j,k,l,maxs:longint;
       p:array[0..10,0..10,0..10,0..10]of longint;
       a:array[0..100,0..100]of longint;
    begin
     readln(n);
     readln(x,y,s);
     fillchar(a,sizeof(a),0);
     while (x>0)or(x>0)or(s>0)do
      begin
       a[x,y]:=s;
       readln(x,y,s);
      end;
     fillchar(p,sizeof(p),0);
     for i:=1 to n do
      for j:=1 to n do
       for k:=1 to n do
        for l:=1 to n do
         begin
          p[i,j,k,l]:=max(p[i,j-1,k,l-1],p[i-1,j,k,l-1]);
          maxs:=max(p[i,j-1,k-1,l],p[i-1,j,k-1,l]);
          if maxs>p[i,j,k,l]then p[i,j,k,l]:=maxs;
          if (i=k)and(j=l)then inc(p[i,j,k,l],a[i,j])
          else p[i,j,k,l]:=p[i,j,k,l]+a[i,j]+a[k,l];
         end;
     writeln(p[n,n,n,n]);
    end.
    
    • 1

    信息

    ID
    206
    时间
    1000ms
    内存
    125MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者