forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat-and-mouse-ii.py
184 lines (172 loc) · 6.32 KB
/
cat-and-mouse-ii.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Time: O((m * n)^2 * (m + n))
# Space: O((m * n)^2)
import collections
class Solution(object):
def canMouseWin(self, grid, catJump, mouseJump):
"""
:type grid: List[str]
:type catJump: int
:type mouseJump: int
:rtype: bool
"""
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
DRAW, MOUSE, CAT = range(3)
def parents(m, c, t):
if t == CAT:
for nm in graph[m, MOUSE^CAT^t]:
yield nm, c, MOUSE^CAT^t
else:
for nc in graph[c, MOUSE^CAT^t]:
yield m, nc, MOUSE^CAT^t
R, C = len(grid), len(grid[0])
N = R*C
WALLS = set()
FOOD, MOUSE_START, CAT_START = [-1]*3
for r in xrange(R):
for c in xrange(C):
if grid[r][c] == 'M':
MOUSE_START = r*C + c
elif grid[r][c] == 'C':
CAT_START = r*C + c
elif grid[r][c] == 'F':
FOOD = r*C + c
elif grid[r][c] == '#':
WALLS.add(r*C + c)
graph = collections.defaultdict(set)
jump = {MOUSE:mouseJump, CAT:catJump}
for r in xrange(R):
for c in xrange(C):
if grid[r][c] == '#':
continue
pos = r*C + c
for t in [MOUSE, CAT]:
for dr, dc in directions:
for d in xrange(jump[t]+1):
nr, nc = r+dr*d, c+dc*d
if not (0 <= nr < R and 0 <= nc < C and grid[nr][nc] != '#'):
break
graph[pos, t].add(nr*C + nc)
degree = {}
for m in xrange(N):
for c in xrange(N):
degree[m, c, MOUSE] = len(graph[m, MOUSE])
degree[m, c, CAT] = len(graph[c, CAT])
color = collections.defaultdict(int)
q = collections.deque()
for i in xrange(N):
if i in WALLS or i == FOOD:
continue
color[FOOD, i, CAT] = MOUSE
q.append((FOOD, i, CAT, MOUSE))
color[i, FOOD, MOUSE] = CAT
q.append((i, FOOD, MOUSE, CAT))
for t in [MOUSE, CAT]:
color[i, i, t] = CAT
q.append((i, i, t, CAT))
while q:
i, j, t, c = q.popleft()
for ni, nj, nt in parents(i, j, t):
if color[ni, nj, nt] != DRAW:
continue
if nt == c:
color[ni, nj, nt] = c
q.append((ni, nj, nt, c))
continue
degree[ni, nj, nt] -= 1
if not degree[ni, nj, nt]:
color[ni, nj, nt] = c
q.append((ni, nj, nt, c))
return color[MOUSE_START, CAT_START, MOUSE] == MOUSE
# Time: O((m * n)^2 * (m + n))
# Space: O((m * n)^2)
import collections
class Solution2(object):
def canMouseWin(self, grid, catJump, mouseJump):
"""
:type grid: List[str]
:type catJump: int
:type mouseJump: int
:rtype: bool
"""
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
DRAW, MOUSE, CAT = range(3)
def parents(m, c, t):
if t == CAT:
for nm in graph[m, MOUSE^CAT^t]:
yield nm, c, MOUSE^CAT^t
else:
for nc in graph[c, MOUSE^CAT^t]:
yield m, nc, MOUSE^CAT^t
R, C = len(grid), len(grid[0])
N = R*C
WALLS = set()
FOOD, MOUSE_START, CAT_START = [-1]*3
for r in xrange(R):
for c in xrange(C):
if grid[r][c] == 'M':
MOUSE_START = r*C + c
elif grid[r][c] == 'C':
CAT_START = r*C + c
elif grid[r][c] == 'F':
FOOD = r*C + c
elif grid[r][c] == '#':
WALLS.add(r*C + c)
graph = collections.defaultdict(set)
jump = {MOUSE:mouseJump, CAT:catJump}
for r in xrange(R):
for c in xrange(C):
if grid[r][c] == '#':
continue
pos = r*C + c
for t in [MOUSE, CAT]:
for dr, dc in directions:
for d in xrange(jump[t]+1):
nr, nc = r+dr*d, c+dc*d
if not (0 <= nr < R and 0 <= nc < C and grid[nr][nc] != '#'):
break
graph[pos, t].add(nr*C + nc)
degree = {}
for m in xrange(N):
for c in xrange(N):
# degree[m, c, MOUSE] = len(graph[m, MOUSE])
degree[m, c, CAT] = len(graph[c, CAT])
color = collections.defaultdict(int)
q1 = collections.deque()
# q2 = collections.deque()
for i in xrange(N):
if i in WALLS or i == FOOD:
continue
color[FOOD, i, CAT] = MOUSE
q1.append((FOOD, i, CAT))
color[i, FOOD, MOUSE] = CAT
# q2.append((i, FOOD, MOUSE))
for t in [MOUSE, CAT]:
color[i, i, t] = CAT
# q2.append((i, i, t))
while q1:
i, j, t = q1.popleft()
for ni, nj, nt in parents(i, j, t):
if color[ni, nj, nt] != DRAW:
continue
if t == CAT:
color[ni, nj, nt] = MOUSE
q1.append((ni, nj, nt))
continue
degree[ni, nj, nt] -= 1
if not degree[ni, nj, nt]:
color[ni, nj, nt] = MOUSE
q1.append((ni, nj, nt))
# while q2:
# i, j, t = q2.popleft()
# for ni, nj, nt in parents(i, j, t):
# if color[ni, nj, nt] != DRAW:
# continue
# if t == MOUSE:
# color[ni, nj, nt] = CAT
# q2.append((ni, nj, nt))
# continue
# degree[ni, nj, nt] -= 1
# if not degree[ni, nj, nt]:
# color[ni, nj, nt] = CAT
# q2.append((ni, nj, nt))
return color[MOUSE_START, CAT_START, MOUSE] == MOUSE