Operations

0

0 votes
Medium
Problem

Given a 4x4 matrix with initial values from 0 to 9 of the following form :
A B C D
E F G H
I J K L
M N O P
where A-P are integer values between 0-9.

There are 16 operations defined on this matrix.
Operation 1: A B C D
Operation 2: A E I M
Operation 3: M N O P
Operation 4: D H L P
Operation 5: A B C E F G I J K
Operation 6: E F G I J K M N O
Operation 7: B C D F G H J K L
Operation 8: F G H J K L N O P
Operation 9: F G J K
Operation 10: B C F G
Operation 11: J K N O
Operation 12: A B E F
Operation 13: C D G H
Operation 14: I J M N
Operation 15: K L O P
Operation 16: C G L P

In each operation, the elements at the corresponding letters mentioned have their value decreased by 1. Find the minimum number of operations required to make all the elements of the matrix 0. If this is impossible, print -1.

Note: Each operation can be used only once during a test case

INPUT:
The first line contains T, the number of test cases. Each test case has 4 lines, each line containing 4 integers defining the matrix. There is a blank line in between test cases.

OUTPUT:
For each test case, print the case number followed by the minimum operations required. If this is impossible, print -1. See sample test case for more clarity.

CONSTRAINTS:
1<=T<=100
0<=elements of matrix<=9

Sample Input
2
2 1 1 1
1 0 0 0
1 0 0 0
1 0 0 0

0 0 0 0
0 0 0 0
0 0 0 0
1 1 1 1
Sample Output
Case #1: 2
Case #2: 1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case, Value of matrix after applying Operation 1 :
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0
Value of matrix after Operation 2:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Hence, minimum operations required is 2.

Editor Image

?