#P2258. [NOIP 2014 普及组] 子矩阵
[NOIP 2014 普及组] 子矩阵
Description
Definitions:
-
Submatrix: A new matrix formed by selecting some rows and some columns from a matrix and taking the elements at their intersections (preserving the relative order of rows and columns) is called a submatrix of the original matrix.
For example, in the table below, selecting rows and columns yields a submatrix as follows.
One of its submatrices is:
-
Adjacent elements: An element in the matrix is adjacent to its four neighbors above, below, left, and right (if they exist).
-
Score of a matrix: The sum of the absolute differences of every pair of adjacent elements in the matrix.
Task: Given a positive-integer matrix with rows and columns, choose an -row -column submatrix from it so that the score of this submatrix is minimized, and output that score.
Input Format
The first line contains four integers separated by single spaces, as described above.
The next lines each contain integers, representing the -row -column matrix described in the problem statement.
Output Format
Output a single integer, the minimum possible score among all submatrices that satisfy the description.
5 5 2 3
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
6
7 7 3 3
7 7 7 6 2 10 5
5 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 10
1 3 1 5 4 8 6
16
Hint
Sample 1 explanation
In this matrix, the -row -column submatrix with the minimum score is formed by the intersection of rows and columns of the original matrix, namely:
Its score is .
Sample 2 explanation
In this matrix, the -row -column submatrix with the minimum score is formed by the intersection of rows and columns of the original matrix. The chosen submatrix with the minimum score is:
Constraints
- For of the testdata, , , and for every element .
- For of the testdata, , , every element satisfies , , and .
Translated by ChatGPT 5
京公网安备 11011102002149号