Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62334f8

Browse files
feat: add solutions to lc problem: No.0913 (#4050)
No.0913.Cat and Mouse
1 parent b9dc687 commit 62334f8

File tree

9 files changed

+660
-183
lines changed

9 files changed

+660
-183
lines changed

‎solution/0900-0999/0913.Cat and Mouse/README.md‎

Lines changed: 219 additions & 39 deletions
Large diffs are not rendered by default.

‎solution/0900-0999/0913.Cat and Mouse/README_EN.md‎

Lines changed: 229 additions & 49 deletions
Large diffs are not rendered by default.

‎solution/0900-0999/0913.Cat and Mouse/Solutioin.py‎

Lines changed: 0 additions & 56 deletions
This file was deleted.

‎solution/0900-0999/0913.Cat and Mouse/Solution.cpp‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Solution {
1111
public:
1212
int catMouseGame(vector<vector<int>>& graph) {
1313
int n = graph.size();
14-
int res[n][n][2];
14+
int ans[n][n][2];
1515
int degree[n][n][2];
16-
memset(res, 0, sizeof res);
16+
memset(ans, 0, sizeof ans);
1717
memset(degree, 0, sizeof degree);
1818
for (int i = 0; i < n; ++i) {
1919
for (int j = 1; j < n; ++j) {
@@ -42,34 +42,34 @@ class Solution {
4242
};
4343
queue<tuple<int, int, int>> q;
4444
for (int j = 1; j < n; ++j) {
45-
res[0][j][MOUSE_TURN] = res[0][j][CAT_TURN] = MOUSE_WIN;
45+
ans[0][j][MOUSE_TURN] = ans[0][j][CAT_TURN] = MOUSE_WIN;
4646
q.emplace(0, j, MOUSE_TURN);
4747
q.emplace(0, j, CAT_TURN);
4848
}
4949
for (int i = 1; i < n; ++i) {
50-
res[i][i][MOUSE_TURN] = res[i][i][CAT_TURN] = CAT_WIN;
50+
ans[i][i][MOUSE_TURN] = ans[i][i][CAT_TURN] = CAT_WIN;
5151
q.emplace(i, i, MOUSE_TURN);
5252
q.emplace(i, i, CAT_TURN);
5353
}
5454
while (!q.empty()) {
5555
auto [m, c, t] = q.front();
5656
q.pop();
57-
int x = res[m][c][t];
57+
int x = ans[m][c][t];
5858
for (auto [pm, pc, pt] : getPrevStates(m, c, t)) {
59-
if (res[pm][pc][pt] == TIE) {
59+
if (ans[pm][pc][pt] == TIE) {
6060
bool win = (x == MOUSE_WIN && pt == MOUSE_TURN) || (x == CAT_WIN && pt == CAT_TURN);
6161
if (win) {
62-
res[pm][pc][pt] = x;
62+
ans[pm][pc][pt] = x;
6363
q.emplace(pm, pc, pt);
6464
} else {
6565
if (--degree[pm][pc][pt] == 0) {
66-
res[pm][pc][pt] = x;
66+
ans[pm][pc][pt] = x;
6767
q.emplace(pm, pc, pt);
6868
}
6969
}
7070
}
7171
}
7272
}
73-
return res[MOUSE_START][CAT_START][MOUSE_TURN];
73+
return ans[MOUSE_START][CAT_START][MOUSE_TURN];
7474
}
75-
};
75+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class Solution {
5+
private int n;
6+
private int[][] g;
7+
private int[,,] ans;
8+
private int[,,] degree;
9+
10+
private const int HOLE = 0, MOUSE_START = 1, CAT_START = 2;
11+
private const int MOUSE_TURN = 0, CAT_TURN = 1;
12+
private const int MOUSE_WIN = 1, CAT_WIN = 2, TIE = 0;
13+
14+
public int CatMouseGame(int[][] graph) {
15+
n = graph.Length;
16+
g = graph;
17+
ans = new int[n, n, 2];
18+
degree = new int[n, n, 2];
19+
20+
for (int i = 0; i < n; i++) {
21+
for (int j = 1; j < n; j++) {
22+
degree[i, j, MOUSE_TURN] = g[i].Length;
23+
degree[i, j, CAT_TURN] = g[j].Length;
24+
}
25+
}
26+
27+
for (int i = 0; i < n; i++) {
28+
foreach (int j in g[HOLE]) {
29+
degree[i, j, CAT_TURN]--;
30+
}
31+
}
32+
33+
Queue<int[]> q = new Queue<int[]>();
34+
35+
for (int j = 1; j < n; j++) {
36+
ans[0, j, MOUSE_TURN] = MOUSE_WIN;
37+
ans[0, j, CAT_TURN] = MOUSE_WIN;
38+
q.Enqueue(new int[] { 0, j, MOUSE_TURN });
39+
q.Enqueue(new int[] { 0, j, CAT_TURN });
40+
}
41+
42+
for (int i = 1; i < n; i++) {
43+
ans[i, i, MOUSE_TURN] = CAT_WIN;
44+
ans[i, i, CAT_TURN] = CAT_WIN;
45+
q.Enqueue(new int[] { i, i, MOUSE_TURN });
46+
q.Enqueue(new int[] { i, i, CAT_TURN });
47+
}
48+
49+
while (q.Count > 0) {
50+
int[] state = q.Dequeue();
51+
int t = ans[state[0], state[1], state[2]];
52+
List<int[]> prevStates = GetPrevStates(state);
53+
54+
foreach (var prevState in prevStates) {
55+
int pm = prevState[0], pc = prevState[1], pt = prevState[2];
56+
if (ans[pm, pc, pt] == TIE) {
57+
bool win = (t == MOUSE_WIN && pt == MOUSE_TURN) || (t == CAT_WIN && pt == CAT_TURN);
58+
if (win) {
59+
ans[pm, pc, pt] = t;
60+
q.Enqueue(prevState);
61+
} else {
62+
if (--degree[pm, pc, pt] == 0) {
63+
ans[pm, pc, pt] = t;
64+
q.Enqueue(prevState);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
return ans[MOUSE_START, CAT_START, MOUSE_TURN];
72+
}
73+
74+
private List<int[]> GetPrevStates(int[] state) {
75+
List<int[]> pre = new List<int[]>();
76+
int m = state[0], c = state[1], t = state[2];
77+
int pt = t ^ 1;
78+
79+
if (pt == CAT_TURN) {
80+
foreach (int pc in g[c]) {
81+
if (pc != HOLE) {
82+
pre.Add(new int[] { m, pc, pt });
83+
}
84+
}
85+
} else {
86+
foreach (int pm in g[m]) {
87+
pre.Add(new int[] { pm, c, pt });
88+
}
89+
}
90+
91+
return pre;
92+
}
93+
}

‎solution/0900-0999/0913.Cat and Mouse/Solution.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const (
1010
)
1111

1212
func catMouseGame(graph [][]int) int {
13-
res := [50][50][2]int{}
13+
ans := [50][50][2]int{}
1414
degree := [50][50][2]int{}
1515
n := len(graph)
1616
for i := 0; i < n; i++ {
@@ -25,12 +25,12 @@ func catMouseGame(graph [][]int) int {
2525
type tuple struct{ m, c, t int }
2626
q := []tuple{}
2727
for j := 1; j < n; j++ {
28-
res[0][j][mouseTurn], res[0][j][catTurn] = mouseWin, mouseWin
28+
ans[0][j][mouseTurn], ans[0][j][catTurn] = mouseWin, mouseWin
2929
q = append(q, tuple{0, j, mouseTurn})
3030
q = append(q, tuple{0, j, catTurn})
3131
}
3232
for i := 1; i < n; i++ {
33-
res[i][i][mouseTurn], res[i][i][catTurn] = catWin, catWin
33+
ans[i][i][mouseTurn], ans[i][i][catTurn] = catWin, catWin
3434
q = append(q, tuple{i, i, mouseTurn})
3535
q = append(q, tuple{i, i, catTurn})
3636
}
@@ -54,23 +54,23 @@ func catMouseGame(graph [][]int) int {
5454
state := q[0]
5555
m, c, t := state.m, state.c, state.t
5656
q = q[1:]
57-
x := res[m][c][t]
57+
x := ans[m][c][t]
5858
for _, prevState := range getPrevStates(m, c, t) {
5959
pm, pc, pt := prevState.m, prevState.c, prevState.t
60-
if res[pm][pc][pt] == tie {
60+
if ans[pm][pc][pt] == tie {
6161
win := (x == mouseWin && pt == mouseTurn) || (x == catWin && pt == catTurn)
6262
if win {
63-
res[pm][pc][pt] = x
63+
ans[pm][pc][pt] = x
6464
q = append(q, tuple{pm, pc, pt})
6565
} else {
6666
degree[pm][pc][pt]--
6767
if degree[pm][pc][pt] == 0 {
68-
res[pm][pc][pt] = x
68+
ans[pm][pc][pt] = x
6969
q = append(q, tuple{pm, pc, pt})
7070
}
7171
}
7272
}
7373
}
7474
}
75-
return res[mouseStart][catStart][mouseTurn]
76-
}
75+
return ans[mouseStart][catStart][mouseTurn]
76+
}

‎solution/0900-0999/0913.Cat and Mouse/Solution.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
private int n;
33
private int[][] g;
4-
private int[][][] res;
4+
private int[][][] ans;
55
private int[][][] degree;
66

77
private static final int HOLE = 0, MOUSE_START = 1, CAT_START = 2;
@@ -11,7 +11,7 @@ class Solution {
1111
public int catMouseGame(int[][] graph) {
1212
n = graph.length;
1313
g = graph;
14-
res = new int[n][n][2];
14+
ans = new int[n][n][2];
1515
degree = new int[n][n][2];
1616
for (int i = 0; i < n; ++i) {
1717
for (int j = 1; j < n; ++j) {
@@ -26,39 +26,39 @@ public int catMouseGame(int[][] graph) {
2626
}
2727
Deque<int[]> q = new ArrayDeque<>();
2828
for (int j = 1; j < n; ++j) {
29-
res[0][j][MOUSE_TURN] = MOUSE_WIN;
30-
res[0][j][CAT_TURN] = MOUSE_WIN;
29+
ans[0][j][MOUSE_TURN] = MOUSE_WIN;
30+
ans[0][j][CAT_TURN] = MOUSE_WIN;
3131
q.offer(new int[] {0, j, MOUSE_TURN});
3232
q.offer(new int[] {0, j, CAT_TURN});
3333
}
3434
for (int i = 1; i < n; ++i) {
35-
res[i][i][MOUSE_TURN] = CAT_WIN;
36-
res[i][i][CAT_TURN] = CAT_WIN;
35+
ans[i][i][MOUSE_TURN] = CAT_WIN;
36+
ans[i][i][CAT_TURN] = CAT_WIN;
3737
q.offer(new int[] {i, i, MOUSE_TURN});
3838
q.offer(new int[] {i, i, CAT_TURN});
3939
}
4040
while (!q.isEmpty()) {
4141
int[] state = q.poll();
42-
int t = res[state[0]][state[1]][state[2]];
42+
int t = ans[state[0]][state[1]][state[2]];
4343
List<int[]> prevStates = getPrevStates(state);
4444
for (var prevState : prevStates) {
4545
int pm = prevState[0], pc = prevState[1], pt = prevState[2];
46-
if (res[pm][pc][pt] == TIE) {
46+
if (ans[pm][pc][pt] == TIE) {
4747
boolean win
4848
= (t == MOUSE_WIN && pt == MOUSE_TURN) || (t == CAT_WIN && pt == CAT_TURN);
4949
if (win) {
50-
res[pm][pc][pt] = t;
50+
ans[pm][pc][pt] = t;
5151
q.offer(prevState);
5252
} else {
5353
if (--degree[pm][pc][pt] == 0) {
54-
res[pm][pc][pt] = t;
54+
ans[pm][pc][pt] = t;
5555
q.offer(prevState);
5656
}
5757
}
5858
}
5959
}
6060
}
61-
return res[MOUSE_START][CAT_START][MOUSE_TURN];
61+
return ans[MOUSE_START][CAT_START][MOUSE_TURN];
6262
}
6363

6464
private List<int[]> getPrevStates(int[] state) {
@@ -78,4 +78,4 @@ private List<int[]> getPrevStates(int[] state) {
7878
}
7979
return pre;
8080
}
81-
}
81+
}

‎solution/0900-0999/0913.Cat and Mouse/Solution.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get_prev_states(state):
1919
return pre
2020

2121
n = len(graph)
22-
res = [[[0, 0] for _ in range(n)] for _ in range(n)]
22+
ans = [[[0, 0] for _ in range(n)] for _ in range(n)]
2323
degree = [[[0, 0] for _ in range(n)] for _ in range(n)]
2424
for i in range(n):
2525
for j in range(1, n):
@@ -29,28 +29,28 @@ def get_prev_states(state):
2929
degree[i][j][CAT_TURN] -= 1
3030
q = deque()
3131
for j in range(1, n):
32-
res[0][j][MOUSE_TURN] = res[0][j][CAT_TURN] = MOUSE_WIN
32+
ans[0][j][MOUSE_TURN] = ans[0][j][CAT_TURN] = MOUSE_WIN
3333
q.append((0, j, MOUSE_TURN))
3434
q.append((0, j, CAT_TURN))
3535
for i in range(1, n):
36-
res[i][i][MOUSE_TURN] = res[i][i][CAT_TURN] = CAT_WIN
36+
ans[i][i][MOUSE_TURN] = ans[i][i][CAT_TURN] = CAT_WIN
3737
q.append((i, i, MOUSE_TURN))
3838
q.append((i, i, CAT_TURN))
3939
while q:
4040
state = q.popleft()
41-
t = res[state[0]][state[1]][state[2]]
41+
t = ans[state[0]][state[1]][state[2]]
4242
for prev_state in get_prev_states(state):
4343
pm, pc, pt = prev_state
44-
if res[pm][pc][pt] == TIE:
44+
if ans[pm][pc][pt] == TIE:
4545
win = (t == MOUSE_WIN and pt == MOUSE_TURN) or (
4646
t == CAT_WIN and pt == CAT_TURN
4747
)
4848
if win:
49-
res[pm][pc][pt] = t
49+
ans[pm][pc][pt] = t
5050
q.append(prev_state)
5151
else:
5252
degree[pm][pc][pt] -= 1
5353
if degree[pm][pc][pt] == 0:
54-
res[pm][pc][pt] = t
54+
ans[pm][pc][pt] = t
5555
q.append(prev_state)
56-
return res[MOUSE_START][CAT_START][MOUSE_TURN]
56+
return ans[MOUSE_START][CAT_START][MOUSE_TURN]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /