1
+ // Runtime: 911 ms (Top 7.06%) | Memory: 70.7 MB (Top 63.53%)
1
2
class Solution {
2
3
int TIME_MAX = 200 ;
3
4
int DRAW = 0 ;
@@ -9,10 +10,10 @@ public int catMouseGame(int[][] graph) {
9
10
10
11
private int dfs (int time , int [] p , int [][] graph , Integer [][][] memo ){ // p[0] -> mouse position, p[1] -> cat position
11
12
Integer old = memo [time ][p [0 ]][p [1 ]];
12
- if (old != null ) return old ; // all the base cases here
13
+ if (old != null ) return old ; // all the base cases here
13
14
if (time >= TIME_MAX ) return DRAW ;
14
- if (p [0 ]==0 ) return MOUSE_WIN ;
15
- if (p [0 ]==p [1 ]) return CAT_WIN ;
15
+ if (p [0 ]==0 ) return MOUSE_WIN ;
16
+ if (p [0 ]==p [1 ]) return CAT_WIN ;
16
17
int state = 0 ;
17
18
int where = p [time &1 ];
18
19
int res = DRAW ;
@@ -21,15 +22,15 @@ private int dfs(int time, int[] p, int[][] graph, Integer[][][] memo){ // p[0] -
21
22
p [time &1 ]=graph [where ][i ];
22
23
state |= 1 << dfs (time +1 , p , graph , memo );
23
24
if ((time &1 )>0 &&(state &4 )>0 || (time &1 )==0 &&(state &2 )>0 ) // if mouse's turn & mouse win
24
- break ; // or cat's turn & cat win, then we stop.
25
+ break ; // or cat's turn & cat win, then we stop.
25
26
}
26
27
}
27
28
p [time &1 ]=where ; // restore p
28
- if (((time &1 )>0 && (state & 4 )>0 )||((time &1 )==0 ) && state ==4 ){
29
+ if (((time &1 )>0 && (state & 4 )>0 )||((time &1 )==0 ) && state ==4 ){
29
30
res = CAT_WIN ; // cat win when (cat's turn & cat win) or (mouse's turn and state = cat)
30
31
}else if (((time &1 )==0 && (state & 2 )>0 )||(time &1 )==1 && state ==2 ){
31
32
res = MOUSE_WIN ; // mouse win when (mouse's turn and mouse win) or (cat's turn and state = mouse)
32
33
}
33
34
return memo [time ][p [0 ]][p [1 ]]=res ;
34
35
}
35
- }
36
+ }
0 commit comments