#P2258. [NOIP 2014 普及组] 子矩阵

    ID: 1224 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp搜索2014NOIp 普及组枚举,暴力剪枝

[NOIP 2014 普及组] 子矩阵

Description

Definitions:

  1. 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 2,42,4 and columns 2,4,52,4,5 yields a 2×32 \times 3 submatrix as follows.

99 3\color{#6a5acd}3 33 3\color{#6a5acd}3 9\color{#6a5acd}9
9\color{#6a5acd}9 4\color{blue}4 8\color{#6a5acd}8 7\color{blue}7 4\color{blue}4
11 7\color{#6a5acd}7 44 6\color{#6a5acd}6
6\color{#6a5acd}6 8\color{blue}8 5\color{#6a5acd}5 6\color{blue}6 9\color{blue}9
77 4\color{#6a5acd}4 55 6\color{#6a5acd}6 1\color{#6a5acd}1

One of its 2×32\times3 submatrices is:

44 77 44
88 66 99
  1. Adjacent elements: An element in the matrix is adjacent to its four neighbors above, below, left, and right (if they exist).

  2. 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 nn rows and mm columns, choose an rr-row cc-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 n,m,r,cn, m, r, c separated by single spaces, as described above.

The next nn lines each contain mm integers, representing the nn-row mm-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 22-row 33-column submatrix with the minimum score is formed by the intersection of rows 4,54, 5 and columns 1,3,41, 3, 4 of the original matrix, namely:

66 55 66
77 55 66

Its score is 65+56+75+56+67+55+66=6|6-5|+|5-6|+|7-5|+|5-6|+|6-7|+|5-5|+|6-6|=6.

Sample 2 explanation

In this matrix, the 33-row 33-column submatrix with the minimum score is formed by the intersection of rows 4,5,64, 5, 6 and columns 2,6,72, 6, 7 of the original matrix. The chosen submatrix with the minimum score is:

99 77 88
99 88 88
55 1010

Constraints

  • For 50%50\% of the testdata, 1n121 \leq n \leq 12, 1m121 \leq m \leq 12, and for every element 1ai,j201 \leq a_{i,j} \leq 20.
  • For 100%100\% of the testdata, 1n161 \leq n \leq 16, 1m161 \leq m \leq 16, every element satisfies 1ai,j10001 \leq a_{i,j} \leq 1000, 1rn1 \leq r \leq n, and 1cm1 \leq c \leq m.

Translated by ChatGPT 5