Problem J
Magic Checkerboard
Consider an
1 |
2 |
3 |
4 |
3 |
4 |
5 |
6 |
5 |
6 |
7 |
8 |
7 |
8 |
9 |
10 |
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 checkboard is invalid, because 2 and 4 share only a corner and have the same parity:
1 |
2 |
4 |
6 |
The first
Input
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
Output
Output a single integer representing the minimum sum
possible by replacing the 0 cells with positive integers to
form a valid Magic Checkerboard. Output
Sample Input 1 | Sample Output 1 |
---|---|
4 4 1 2 3 0 0 0 5 6 0 0 7 8 7 0 0 10 |
88 |
Sample Input 2 | Sample Output 2 |
---|---|
4 4 1 2 3 0 0 0 5 6 0 4 7 8 7 0 0 10 |
-1 |
Sample Input 3 | Sample Output 3 |
---|---|
2 3 0 0 0 0 0 0 |
18 |