3 条题解

  • 3
    @ 2021-10-10 11:28:53
    #include <iostream>
    
    using namespace std;
    const int N = 60;
    int a[N][N];
    int dp[N][N][N][N];
    int main(){
    	int n, m;
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++){
    		for(int j = 1; j <= m; j++){
    			cin >> a[i][j];
    		}
    	}
    	for(int i = 1; i <= n; i++){
    		for(int j = 1; j <= m; j++){
    			for(int x = 1; x <= n; x ++){
    				for(int y = 1; y <= m; y++){
    					int *t;
    					t = &dp[i][j][x][y];
    					*t = max(*t, dp[i-1][j][x][y-1]);
    					*t = max(*t, dp[i-1][j][x-1][y]);
    					*t = max(*t, dp[i][j-1][x-1][y]);
    					*t = max(*t, dp[i][j-1][x][y-1]);
    					*t += a[i][j] + a[x][y];
    					if(i == x && j == y)
    						*t -= a[x][y];
    				}
    			}
    		}
    	
    	}
    	cout << dp[n][m][n][m];
    } 
    
    • 0
      @ 2023-12-10 20:47:50
      #include<bits/stdc++.h>
      using namespace std;
      const int N=55;
      int m,n,a[N][N],dp[N][N][N];
      int main(){
          cin>>m>>n;
          for(int i=1;i<=m;i++){
              for(int j=1;j<=n;j++){
                  cin>>a[i][j];
              }
          }
          for(int i=1;i<=m;i++)
              for(int j=1;j<=n;j++)
                  for(int k=1;k<=m;k++){
                      int l=i+j-k;
                      if(l>=1&&l<=n) {
                          int temp=max(max(dp[i-1][j][k-1],dp[i-1][j][k]),max(dp[i][j-1][k-1],dp[i][j-1][k]));
                          dp[i][j][k]=temp+a[i][j];
                          if(i!=k) dp[i][j][k]+=a[k][l];
                      }
                  }
          cout<<dp[m][n][m]<<endl;
          return 0;
      }
      
      • -4
        @ 2021-8-8 1:33:00

        C++ :

        #include<bits/stdc++.h>
        using namespace std;
        int a[51][51],n,m;
        unsigned int s[51][51][51][51];
        int main(int argc,char*argv[]){
            memset(a,0,sizeof(a));
            memset(s,0,sizeof(s));
            scanf("%d%d",&m,&n);
            for(int i=1;i<=m;i++){
            	for(int j=1;j<=n;j++){
            		 scanf("%d",&a[i][j]); 
        		}   
        		int a1,a2;
            	for(int j=1;j<=n;j++){
            		for(int k=1;k<=m;k++){
            			for(int l=1;l<=n;l++){
                    		a1=max(s[i][j-1][k][l-1],s[i-1][j][k][l-1]);
                    		a2=max(s[i-1][j][k-1][l],s[i][j-1][k-1][l]);
                    		s[i][j][k][l]=max(a1,a2)+a[i][j]+a[k][l];
                    		if(i==k&&j==l)s[i][j][k][l]-=a[i][j];
                		}
        			}
        		}
        	}
            printf("%d",s[m][n][m][n]);
            return 0;
        }
        
        • 1

        信息

        ID
        186
        时间
        1000ms
        内存
        128MiB
        难度
        1
        标签
        递交数
        41
        已通过
        31
        上传者