#P16166. [ICPC 2015 NAIPC] Magic Checkerboard
[ICPC 2015 NAIPC] Magic Checkerboard
题目描述
Consider an checkerboard. On each cell of the checkerboard, place a positive integer. The values in each column of the checkerboard must be in strictly increasing order from top to bottom, and the values in each row of the checkerboard must be in strictly increasing order from left to right.
$$\begin{array}{|c|c|c|c|} \hline 1 & 2 & 3 & 4 \\ \hline 3 & 4 & 5 & 6 \\ \hline 5 & 6 & 7 & 8 \\ \hline 7 & 8 & 9 & 10 \\ \hline \end{array}$$A Magic Checkerboard has an additional constraint. The cells that share only a corner must have numbers of different parity (Even vs Odd). Note that the following checkerboard is invalid, because and share only a corner and have the same parity:
$$\begin{array}{|c|c|} \hline 1 & 2 \\ \hline 4 & 6 \\ \hline \end{array}$$The first example is a valid Magic Checkerboard. Given a partially filled magic checkerboard, can you fill the remaining locations on the checkerboard, so that the sum of all values is as small as possible?
输入格式
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each input starts with a line with two space-separated integers and (), representing the number of rows () and the number of columns () of the checkerboard. Each of the next lines will contain space-separated integers (), representing the contents of the checkerboard. Zero is used for cells without numbers that you must fill in. You may use any positive integers to fill in the cells without numbers, so long as you form a valid Magic Checkerboard. You are not limited to numbers , and the numbers are not required to be unique.
输出格式
Output a single integer representing the minimum sum possible by replacing the cells with positive integers to form a valid Magic Checkerboard. Output if it is not possible to replace the cells to meet the constraints of a Magic Checkerboard.
4 4
1 2 3 0
0 0 5 6
0 0 7 8
7 0 0 10
88
4 4
1 2 3 0
0 0 5 6
0 4 7 8
7 0 0 10
-1
2 3
0 0 0
0 0 0
18