-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path913.java
More file actions
166 lines (151 loc) · 5.59 KB
/
913.java
File metadata and controls
166 lines (151 loc) · 5.59 KB
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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
//mouse goes firt & starts at 1
// cat starts at 2 & cant go to 0
private static final int MOUSE_WIN = 1;
private static final int CAT_WIN = 2;
private static final int TIE = -1; //
public int catMouseGame(int[][] graph) {
int n = graph.length;
int[][] memo = new int[n][n];
memo[0][0] = MOUSE_WIN;
for (int i=1; i<n;i++) {
memo[0][i] = MOUSE_WIN;
memo[i][i] = CAT_WIN;
}
int ans = dfs(graph, 1,2, memo);
return ans == -1 ? 0: ans;
}
private int dfs(int[][] graph, int mouse, int cat, int[][] memo) {
if (memo[mouse][cat] != 0)
return memo[mouse][cat];
memo[mouse][cat] = TIE;
int[] mouseAdjacent = graph[mouse];
int[] catAdjacent = graph[cat];
int mouseDefault = CAT_WIN;
for (int mN : mouseAdjacent) {
if (mN == cat) continue; // dont go there
int catDefault = MOUSE_WIN;
for (int cN: catAdjacent) {
if (cN == 0)
continue; // not allowed
int result = dfs(graph, mN, cN, memo);
if (result == CAT_WIN) {
catDefault = CAT_WIN;
break;
} else if (result == TIE) {
catDefault = TIE;
}
}
if (catDefault == MOUSE_WIN) {
mouseDefault = MOUSE_WIN; // mouse win, doesn't need to continue
break;
}
if (catDefault == TIE) {
mouseDefault = TIE;
}
}
memo[mouse][cat] = mouseDefault;
return memo[mouse][cat];
}
}
__________________________________________________________________________________________________
sample 37524 kb submission
import java.lang.AssertionError;
import java.util.*;
import java.util.regex.*;
class Solution {
static final int MOUSE_TURN = 0;
static final int CAT_TURN = 1;
static final int STATE_INIT = 0;
static final int STATE_MOUSE_WIN = 1;
static final int STATE_CAT_WIN = 2;
public int catMouseGame(int[][] graph) {
int size = graph.length;
int[][][] stateAAA = new int[size][size][2]; // mouse,cat,turn
// put cat=mouse -> cat win
for(int m=0;m<size;++m){
int c=m;
for(int t=0;t<2;++t){
stateAAA[m][c][t] = STATE_CAT_WIN;
}
}
// put mouse=0 -> mouse win
{
int m=0;
for(int c=0;c<size;++c){
for(int t=0;t<2;++t){
stateAAA[m][c][t] = STATE_MOUSE_WIN;
}
}
}
// put cat=0 -> mouse win (forbid move)
{
int c=0;
for(int m=0;m<size;++m){
for(int t=0;t<2;++t){
stateAAA[m][c][t] = STATE_MOUSE_WIN;
}
}
}
// dijkstra?
while(true){
boolean change = false;
// mouse move
{
int t=MOUSE_TURN;
for(int m=0;m<size;++m){
for(int c=0;c<size;++c){
if(stateAAA[m][c][t]!=STATE_INIT){continue;}
boolean existMouseWin = false;
boolean allCatWin = true;
int[] nextMAry = graph[m];
for(int nextM:nextMAry){
int next = stateAAA[nextM][c][1-t];
if(next==STATE_MOUSE_WIN){existMouseWin=true;break;}
if(next!=STATE_CAT_WIN){allCatWin=false;}
}
if(existMouseWin){
stateAAA[m][c][t] = STATE_MOUSE_WIN;
change=true;
}else if(allCatWin){
stateAAA[m][c][t] = STATE_CAT_WIN;
change=true;
}
}
}
}
// cat move
{
int t=CAT_TURN;
for(int m=0;m<size;++m){
for(int c=0;c<size;++c){
if(stateAAA[m][c][t]!=STATE_INIT){continue;}
boolean existCatWin = false;
boolean allMouseWin = true;
int[] nextCAry = graph[c];
for(int nextC:nextCAry){
int next = stateAAA[m][nextC][1-t];
if(next==STATE_CAT_WIN){existCatWin=true;break;}
if(next!=STATE_MOUSE_WIN){allMouseWin=false;}
}
if(existCatWin){
stateAAA[m][c][t] = STATE_CAT_WIN;
change=true;
}else if(allMouseWin){
stateAAA[m][c][t] = STATE_MOUSE_WIN;
change=true;
}
}
}
}
if(!change)break;
if(stateAAA[1][2][MOUSE_TURN] != STATE_INIT)break;
}
if(stateAAA[1][2][MOUSE_TURN] == STATE_MOUSE_WIN) return 1;
if(stateAAA[1][2][MOUSE_TURN] == STATE_CAT_WIN) return 2;
return 0;
}
}
__________________________________________________________________________________________________