forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutioin.py
39 lines (38 loc) · 1.26 KB
/
Solutioin.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
class Solution:
def catMouseGame(self, graph: List[List[int]]) -> int:
@lru_cache(None)
def dfs(i, j, k):
# mouse / cat / steps
if k >= 2 * len(graph):
return 0 # tie
if i == 0:
return 1 # mouse wins
if i == j:
return 2 # cat wins
if k % 2: # cat’s turn
tie = False
for next in graph[j]:
if next == 0:
continue
x = dfs(i, next, k + 1)
if x == 2:
return 2
if x == 0:
# continue to find if exists winning case
tie = True
if tie:
return 0
return 1
else: # mouse's turn
tie = False
for next in graph[i]:
x = dfs(next, j, k + 1)
if x == 1:
return 1
if x == 0:
# continue to find if exists winning case
tie = True
if tie:
return 0
return 2
return dfs(1, 2, 0)