-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcheckboard-matrix.py
71 lines (57 loc) · 1.52 KB
/
checkboard-matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) 2015 kamyu. All rights reserved.
#
# Google Code Jam 2014 World Finals - Problem A. Checkboard Matrix
# https://code.google.com/codejam/contest/7214486/dashboard#s=p0
#
# Time: O(N^2)
# Space: O(N^2)
#
def count_swaps(pos):
nswaps = 0
for i in pos:
if i % 2 == 0:
nswaps += 1
return nswaps
def inverse(A):
return [chr(ord('0') + ord('1') - ord(c)) for c in A]
# min_swaps returns minimum swaps required to
# form alternating rows. If B is an invalid matrix,
# returns -1 to denote an impossible case.
def min_swaps(M, N):
# Step 1.
typeA = M[0]
typeB = inverse(typeA)
pos_A = []
pos_B = []
for i in xrange(2 * N):
# Step 2 a.
if M[i] == typeA:
pos_A.append(i)
# Step 2 b.
elif M[i] == typeB:
pos_B.append(i)
# Step 2 c.
else:
return -1
# Step 3.
if len(pos_A) != len(pos_B):
return -1
# Step 4.
return min(count_swaps(pos_A), count_swaps(pos_B))
def checkboard_matrix(M, N):
# Step 1-4.
row_swaps = min_swaps(M, N)
if row_swaps == -1:
return "IMPOSSIBLE"
Mt = [list(i) for i in zip(*M)] # Transpose matrix M
# Step 5.
col_swaps = min_swaps(Mt, N)
if col_swaps == -1:
return "IMPOSSIBLE"
return row_swaps + col_swaps
for case in xrange(input()):
N = input()
M = []
for i in xrange(2 * N):
M.append(list(raw_input().strip()))
print "Case #%d: %s" % (case+1, checkboard_matrix(M, N))