Luo Gu -P2258 sub-matrix

Matrices

Title Description

Given the following definition:

  1. Matrices: Select a new matrix composed of certain rows and certain columns of a matrix from which the intersection position (relative order to maintain row and column) is called a sub-matrix of the original matrix.

For example, select the second left below 2 2 . 4 4 rows and 2 2 . 4 4 and 5 the element 5 to obtain a crossing position 2 \ 3 Times 2 × FIG. 3 as the right sub-matrix.

9 3 3 3 9

9 4 8 7 4

1 7 4 6 6

6 8 5 6 9

7 4 5 6 1

Wherein a 2 \ 3 Times 2 × submatrix 3 is

4 7 4

8 6 9

  1. Adjacent elements: an element of the matrix with its up and down around the four elements (if any) are adjacent.

  2. Score matrix: the matrix and the absolute values ​​of the difference of each of the adjacent elements.

The title task: Given a n rows and m columns matrix positive integer, please select a matrix from the r row sub-matrix of column c, such that the minimum value of the sub-matrix, and outputs the value.

Topic links: https://www.luogu.com.cn/problem/P2258

Problem-solving ideas: the rows enumerate (dfs determine which rows selected, the columns dp) can be solved by dp

  • #include <bits / STDC ++ H.>
     the using  namespace STD;
     const  int MAXN = 60 ;
     const  int INF = 0x3f3f3f3f ;
     int n-, m, R & lt, C, ANS;
     int R & lt [MAXN], cost [MAXN] [MAXN], DP [MAXN] [MAXN], a [MAXN] [MAXN], Val [MAXN];
     // R & lt need to select the row which represents, cost represents the added value selecting two
     // Val column indicates the increased value selection , dp [i] [j] denotes the front i column select the energy needed j column value 
    int getdp () 
    { 
        int RES = INF;
         for ( int i = . 1 ; i <= m; i ++ ) 
        { 
            Val [i] = 0 ;
            for(int j=1;j<r;j++)
            {
                val[i]+=abs(a[R[j]][i]-a[R[j+1]][i]);
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=i+1;j<=m;j++)
            {
                cost[i][j]=0;
                for(int k=1;k<=r;k++)
                {
                    cost[i][j]+=abs(a[R[k]][i]-A [R & lt [K]] [J]); 
                } 
            } 
        } 
        for ( int i = . 1 ; i <= m; i ++) // assumed that the i-th column has been selected, only dp i-1 j- selected column before a required minimum value to 
        {
             for ( int J = . 1 ; J <= I && J <= C; J ++ ) 
            { 
                DP [I] [J] = 1E9;
                 for ( int K = J- . 1 ; K <I ; K ++ ) 
                  DP [I] [J] = min (DP [I] [J], DP [K] [J- . 1 ] + cost [K] [I] + Val [I]); 
            } 
        } 
        for ( int i=c;i<=m;i++) res=min(res,dp[i][c]);
        return res;
    }
    void dfs(int now,int cnt)
    {
        if(now>n)
        {
            if(cnt==r) ans=min(ans,getdp());
            return ;
        }
        dfs(now+1,cnt);
        R[cnt+1]=now;
        dfs(now+1,cnt+1);
        return ;
    }
    int main()
    {
        cin>>n>>m>>r>>c;
         for(int i=1;i<=n;i++)
         {
             for(int j=1;j<=m;j++)
             {
                  cin>>a[i][j];
             }
    
         }
         ans=inf;
         dfs(1,0);
         cout<<ans<<endl;
         return 0;
    }

     

 

Guess you like

Origin www.cnblogs.com/tombraider-shadow/p/12038919.html