File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ # 913. Cat and Mouse
2+ # https://leetcode.com/problems/cat-and-mouse/
3+ # Dunno
4+ # 05/12/2025
5+
6+
7+ # Note: This is sadly TLE
8+ # 70 cases passed
9+ class Solution :
10+ def catMouseGame (self , graph ) -> int :
11+ from functools import cache
12+ MOUSE_WINS = 1
13+ CAT_WINS = 2
14+ DRAW = 0
15+ REFUGE = 0
16+ limit = 4 * len (graph )
17+
18+ def wins (isCat ):
19+ if isCat : return CAT_WINS
20+ else :
21+ return MOUSE_WINS
22+
23+ @cache
24+ def solve (mouse = 1 , cat = 2 , isCat = False , turns = 0 ):
25+ if mouse == cat : return CAT_WINS
26+ if mouse == REFUGE : return MOUSE_WINS
27+ if turns >= limit : return DRAW
28+
29+ newMouse = mouse
30+ newCat = cat
31+ foundDraw = False
32+ for nxt in graph [cat if isCat else mouse ]:
33+ if isCat :
34+ if nxt == 0 : continue
35+ newCat = nxt
36+ else :
37+ newMouse = nxt
38+ res = solve (newMouse , newCat , not isCat , turns + 1 )
39+ if res == wins (isCat ):
40+ return res
41+
42+ if res == DRAW :
43+ foundDraw = True
44+
45+ if foundDraw :
46+ return DRAW
47+
48+ return wins (not isCat )
49+
50+ return solve ()
You can’t perform that action at this time.
0 commit comments