Skip to content

Commit cd3c36b

Browse files
authored
Create 913-cat-and-mouse.js
1 parent d33555b commit cd3c36b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

913-cat-and-mouse.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @param {number[][]} graph
3+
* @return {number}
4+
*/
5+
const catMouseGame = function (g) {
6+
const n = g.length
7+
const win = Array(2)
8+
.fill(0)
9+
.map(() => Array(n * n).fill(0))
10+
for (let i = 0; i < n; i++) {
11+
win[0][i] = 1
12+
win[1][i] = 1
13+
}
14+
for (let i = 1; i < n; i++) {
15+
win[0][i * n + i] = 2
16+
win[1][i * n + i] = 2
17+
}
18+
19+
while (true) {
20+
let anew = false
21+
for (let m = 0; m < n; m++) {
22+
inner: for (let c = 1; c < n; c++) {
23+
if (win[0][m * n + c] == 0) {
24+
let und = false
25+
for (let e of g[m]) {
26+
if (win[1][e * n + c] == 1) {
27+
win[0][m * n + c] = 1
28+
anew = true
29+
continue inner
30+
}
31+
if (win[1][e * n + c] == 0) {
32+
und = true
33+
}
34+
}
35+
if (!und) {
36+
win[0][m * n + c] = 2
37+
anew = true
38+
}
39+
}
40+
}
41+
}
42+
for (let c = 1; c < n; c++) {
43+
inner: for (let m = 0; m < n; m++) {
44+
if (win[1][m * n + c] == 0) {
45+
let und = false
46+
for (e of g[c]) {
47+
if (e == 0) continue
48+
if (win[0][m * n + e] == 2) {
49+
win[1][m * n + c] = 2
50+
anew = true
51+
continue inner
52+
}
53+
if (win[0][m * n + e] == 0) {
54+
und = true
55+
}
56+
}
57+
if (!und) {
58+
win[1][m * n + c] = 1
59+
anew = true
60+
}
61+
}
62+
}
63+
}
64+
if (!anew) break
65+
}
66+
67+
return win[0][1 * n + 2]
68+
}

0 commit comments

Comments
 (0)