P2258 sub-matrix, p2258 Matrix

Source: Internet
Author: User

P2258 sub-matrix, p2258 Matrix
Description

The following definitions are provided:

For example, in the left figure below, a child matrix of 2*3 is obtained by selecting the elements at the intersection of 2nd, 4, 2nd, 4, and 5 columns, as shown in the right figure.

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

One of the 2*3 sub-matrices of is

4 7 4

8 6 9

  1. Adjacent Elements: an element in the matrix is adjacent to the four elements (if any) at the top, bottom, and right of the matrix.

  2. Matrix score: the sum of the absolute values of the difference between each adjacent element in the matrix.

Task of this question: given a positive integer matrix of n rows and m columns, please select a child matrix of column c in the r row from this matrix to minimize the score of this child matrix and output this score.

(This topic is T4 4noip universal T4)

Input/Output Format Input Format:

The first line contains four integers n, m, r, and c separated by spaces. The meaning of each integer is separated by a space as described in the Problem description.

In the next n rows, each line contains m integers separated by spaces, which are used to represent the matrix of the n rows of m columns in the Problem description.

Output Format:

The output contains one row and one integer, indicating the minimum score of the Child matrix that meets the topic description.

Input and Output sample Input example #1:
5 5 2 39 3 3 3 99 4 8 7 41 7 4 6 66 8 5 6 97 4 5 6 1
Output sample #1:
6
Input example #2:
7 7 3 3  7 7 7 6 2 10 55 8 8 2 1 6 2 2 9 5 5 6 1 7 7 9 3 6 1 7 8 1 9 1 4 7 8 8 10 5 9 1 1 8 101 3 1 5 4 8 6
Output sample #2:
16
Description

[Input and Output Example 1]

The child matrix of the two rows and three columns with the smallest score in this matrix is composed of the elements of the 4th rows of the original matrix, 5th rows and 1st columns, 3rd columns, and 4th columns.

6 5 6

7 5 6

, The value is

| 6−5 | + | 5−6 | + | 7−5 | + | 5−6 | + | 6−7 | + | 5−5 | + | 6−6 | = 6.

[Example 2 of input and output]

The child matrix of the three rows and three columns with the smallest score in this matrix is composed of the elements of the 4th rows, 5th rows, 6th rows and 2nd columns, 6th columns, and 7th columns of the original matrix, the child matrix with the smallest score is

9 7 8 9 8 8 5 8 10

[Data description]

For 50% of the data, 1 ≤ n ≤ 12, 1 ≤ m ≤ 12, each element in the Matrix 1 ≤ a [I] [j] ≤ 20;

For 100% of the data, 1≤n ≤ 16, 1≤m ≤ 16, each element in the matrix 1≤a [I] [j] ≤1,000,

1 ≤ r ≤ n, 1 ≤ c ≤ m.

 

A question about changing outlook on life

First, brute force search

Then the dp Column

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 using namespace std; 7 int n,m,r,c,ans=0x7fff; 8 int a[17][17],dp[17][17],how[17]; 9 void dpans()10 {11     memset(dp,0,sizeof(dp));12     int hangcha=0,liecha=0;13     for(int i=1;i<=c;i++)14     {15         for(int j=0;j<m;j++)16         {17             dp[i][j]=1e9;hangcha=0;18             for(int k=1;k<r;k++)19             20             hangcha+=abs(a[how[k]][j]-a[how[k-1]][j]);21             22             if(i<2)23             {24                 dp[i][j]=hangcha;25                 if(i==c)26                 ans=min(ans,dp[i][j]);27                 continue;28             }29             for(int k=0;k<j;k++)30             {31                 liecha=0;32                 for(int l=0;l<r;l++)33                 liecha+=abs(a[how[l]][k]-a[how[l]][j]);34                 if(i<2)liecha=0;35                 if(dp[i-1][k]+hangcha+liecha<dp[i][j])36                 dp[i][j]=dp[i-1][k]+hangcha+liecha;37             }38             if(i==c)39             ans=min(ans,dp[i][j]);40         }41     }42 }43 void dfs(int x,int num)44 {45     if(num>r)return ;46     if(x==n)47     {48         if(num<r)return ;49         dpans();50         return ;51     }52     how[num]=x;53     dfs(x+1,num+1);54     dfs(x+1,num);55 }56 int main()57 {58     scanf("%d%d%d%d",&n,&m,&r,&c);59     for(int i=0;i<n;i++)60         for(int j=0;j<m;j++)61             scanf("%d",&a[i][j]);62     dfs(0,0);63     printf("%d",ans);64     return 0;65 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.