-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh1284 converted for booleans.py
73 lines (60 loc) · 2.43 KB
/
h1284 converted for booleans.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
72
73
# Created this cause in theory for larger matrices it would be faster to use booleans instead of integers
# But in this case it doesn't matter since we're restricted to 3x3s
class Solution:
def minFlips(self, mat: List[List[int]]) -> int:
self.visited = {}
zeroOneBoolMap = {0: False, 1: True}
newMat = [[zeroOneBoolMap[x] for x in row] for row in mat]
self.case(newMat, sum([sum(x) for x in mat]), 0)
return self.visited.get(('0' * len(mat) * len(mat[0])), -1)
def case(self, mat: List[List[bool]], ones: int, steps: int) -> bool:
hashingMat = self.toString(mat)
if not ones :
self.visited[hashingMat] = min(self.visited.get(hashingMat, inf), steps)
return True
if hashingMat in self.visited :
if self.visited.get(hashingMat) < steps :
return -1
self.visited[hashingMat] = steps
steps += 1
for i in range(len(mat)) :
for j in range(len(mat[0])) :
tempVal = self.flip(mat, i, j)
tempVal = self.case(mat, ones + tempVal, steps)
self.flip(mat, i, j)
if tempVal:
return False # zeros found so any more steps than this will not improve #steps
return False
def flip(self, mat: List[List[int]], x: int, y: int) -> int : # ret # of new 1's
newOnes, newZeros = 0, 0
mat[x][y] = not mat[x][y]
newOnes += mat[x][y]
newZeros += not mat[x][y]
if x > 0 :
mat[x - 1][y] = not mat[x - 1][y]
if mat[x - 1][y] :
newOnes += 1
else :
newZeros += 1
if x < len(mat) - 1 :
mat[x + 1][y] = not mat[x + 1][y]
if mat[x + 1][y] :
newOnes += 1
else :
newZeros += 1
if y > 0 :
mat[x][y - 1] = not mat[x][y - 1]
if mat[x][y - 1] :
newOnes += 1
else :
newZeros += 1
if y < len(mat[0]) - 1 :
mat[x][y + 1] = not mat[x][y + 1]
if matmat[x][y + 1] :
newOnes += 1
else :
newZeros += 1
return newOnes - newZeros
def toString(self, mat: List[List[bool]]) -> str :
zeroOnesToBool = {False: '0', True: '1'}
return ''.join(''.join([zeroOnesToBool[y] for y in row]) for row in mat)