Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0913 #4050

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 219 additions & 39 deletions solution/0900-0999/0913.Cat and Mouse/README.md

Large diffs are not rendered by default.

278 changes: 229 additions & 49 deletions solution/0900-0999/0913.Cat and Mouse/README_EN.md

Large diffs are not rendered by default.

56 changes: 0 additions & 56 deletions solution/0900-0999/0913.Cat and Mouse/Solutioin.py

This file was deleted.

20 changes: 10 additions & 10 deletions solution/0900-0999/0913.Cat and Mouse/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Solution {
public:
int catMouseGame(vector<vector<int>>& graph) {
int n = graph.size();
int res[n][n][2];
int ans[n][n][2];
int degree[n][n][2];
memset(res, 0, sizeof res);
memset(ans, 0, sizeof ans);
memset(degree, 0, sizeof degree);
for (int i = 0; i < n; ++i) {
for (int j = 1; j < n; ++j) {
Expand Down Expand Up @@ -42,34 +42,34 @@ class Solution {
};
queue<tuple<int, int, int>> q;
for (int j = 1; j < n; ++j) {
res[0][j][MOUSE_TURN] = res[0][j][CAT_TURN] = MOUSE_WIN;
ans[0][j][MOUSE_TURN] = ans[0][j][CAT_TURN] = MOUSE_WIN;
q.emplace(0, j, MOUSE_TURN);
q.emplace(0, j, CAT_TURN);
}
for (int i = 1; i < n; ++i) {
res[i][i][MOUSE_TURN] = res[i][i][CAT_TURN] = CAT_WIN;
ans[i][i][MOUSE_TURN] = ans[i][i][CAT_TURN] = CAT_WIN;
q.emplace(i, i, MOUSE_TURN);
q.emplace(i, i, CAT_TURN);
}
while (!q.empty()) {
auto [m, c, t] = q.front();
q.pop();
int x = res[m][c][t];
int x = ans[m][c][t];
for (auto [pm, pc, pt] : getPrevStates(m, c, t)) {
if (res[pm][pc][pt] == TIE) {
if (ans[pm][pc][pt] == TIE) {
bool win = (x == MOUSE_WIN && pt == MOUSE_TURN) || (x == CAT_WIN && pt == CAT_TURN);
if (win) {
res[pm][pc][pt] = x;
ans[pm][pc][pt] = x;
q.emplace(pm, pc, pt);
} else {
if (--degree[pm][pc][pt] == 0) {
res[pm][pc][pt] = x;
ans[pm][pc][pt] = x;
q.emplace(pm, pc, pt);
}
}
}
}
}
return res[MOUSE_START][CAT_START][MOUSE_TURN];
return ans[MOUSE_START][CAT_START][MOUSE_TURN];
}
};
};
93 changes: 93 additions & 0 deletions solution/0900-0999/0913.Cat and Mouse/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;

public class Solution {
private int n;
private int[][] g;
private int[,,] ans;
private int[,,] degree;

private const int HOLE = 0, MOUSE_START = 1, CAT_START = 2;
private const int MOUSE_TURN = 0, CAT_TURN = 1;
private const int MOUSE_WIN = 1, CAT_WIN = 2, TIE = 0;

public int CatMouseGame(int[][] graph) {
n = graph.Length;
g = graph;
ans = new int[n, n, 2];
degree = new int[n, n, 2];

for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
degree[i, j, MOUSE_TURN] = g[i].Length;
degree[i, j, CAT_TURN] = g[j].Length;
}
}

for (int i = 0; i < n; i++) {
foreach (int j in g[HOLE]) {
degree[i, j, CAT_TURN]--;
}
}

Queue<int[]> q = new Queue<int[]>();

for (int j = 1; j < n; j++) {
ans[0, j, MOUSE_TURN] = MOUSE_WIN;
ans[0, j, CAT_TURN] = MOUSE_WIN;
q.Enqueue(new int[] { 0, j, MOUSE_TURN });
q.Enqueue(new int[] { 0, j, CAT_TURN });
}

for (int i = 1; i < n; i++) {
ans[i, i, MOUSE_TURN] = CAT_WIN;
ans[i, i, CAT_TURN] = CAT_WIN;
q.Enqueue(new int[] { i, i, MOUSE_TURN });
q.Enqueue(new int[] { i, i, CAT_TURN });
}

while (q.Count > 0) {
int[] state = q.Dequeue();
int t = ans[state[0], state[1], state[2]];
List<int[]> prevStates = GetPrevStates(state);

foreach (var prevState in prevStates) {
int pm = prevState[0], pc = prevState[1], pt = prevState[2];
if (ans[pm, pc, pt] == TIE) {
bool win = (t == MOUSE_WIN && pt == MOUSE_TURN) || (t == CAT_WIN && pt == CAT_TURN);
if (win) {
ans[pm, pc, pt] = t;
q.Enqueue(prevState);
} else {
if (--degree[pm, pc, pt] == 0) {
ans[pm, pc, pt] = t;
q.Enqueue(prevState);
}
}
}
}
}

return ans[MOUSE_START, CAT_START, MOUSE_TURN];
}

private List<int[]> GetPrevStates(int[] state) {
List<int[]> pre = new List<int[]>();
int m = state[0], c = state[1], t = state[2];
int pt = t ^ 1;

if (pt == CAT_TURN) {
foreach (int pc in g[c]) {
if (pc != HOLE) {
pre.Add(new int[] { m, pc, pt });
}
}
} else {
foreach (int pm in g[m]) {
pre.Add(new int[] { pm, c, pt });
}
}

return pre;
}
}
18 changes: 9 additions & 9 deletions solution/0900-0999/0913.Cat and Mouse/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
)

func catMouseGame(graph [][]int) int {
res := [50][50][2]int{}
ans := [50][50][2]int{}
degree := [50][50][2]int{}
n := len(graph)
for i := 0; i < n; i++ {
Expand All @@ -25,12 +25,12 @@ func catMouseGame(graph [][]int) int {
type tuple struct{ m, c, t int }
q := []tuple{}
for j := 1; j < n; j++ {
res[0][j][mouseTurn], res[0][j][catTurn] = mouseWin, mouseWin
ans[0][j][mouseTurn], ans[0][j][catTurn] = mouseWin, mouseWin
q = append(q, tuple{0, j, mouseTurn})
q = append(q, tuple{0, j, catTurn})
}
for i := 1; i < n; i++ {
res[i][i][mouseTurn], res[i][i][catTurn] = catWin, catWin
ans[i][i][mouseTurn], ans[i][i][catTurn] = catWin, catWin
q = append(q, tuple{i, i, mouseTurn})
q = append(q, tuple{i, i, catTurn})
}
Expand All @@ -54,23 +54,23 @@ func catMouseGame(graph [][]int) int {
state := q[0]
m, c, t := state.m, state.c, state.t
q = q[1:]
x := res[m][c][t]
x := ans[m][c][t]
for _, prevState := range getPrevStates(m, c, t) {
pm, pc, pt := prevState.m, prevState.c, prevState.t
if res[pm][pc][pt] == tie {
if ans[pm][pc][pt] == tie {
win := (x == mouseWin && pt == mouseTurn) || (x == catWin && pt == catTurn)
if win {
res[pm][pc][pt] = x
ans[pm][pc][pt] = x
q = append(q, tuple{pm, pc, pt})
} else {
degree[pm][pc][pt]--
if degree[pm][pc][pt] == 0 {
res[pm][pc][pt] = x
ans[pm][pc][pt] = x
q = append(q, tuple{pm, pc, pt})
}
}
}
}
}
return res[mouseStart][catStart][mouseTurn]
}
return ans[mouseStart][catStart][mouseTurn]
}
24 changes: 12 additions & 12 deletions solution/0900-0999/0913.Cat and Mouse/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
private int n;
private int[][] g;
private int[][][] res;
private int[][][] ans;
private int[][][] degree;

private static final int HOLE = 0, MOUSE_START = 1, CAT_START = 2;
Expand All @@ -11,7 +11,7 @@ class Solution {
public int catMouseGame(int[][] graph) {
n = graph.length;
g = graph;
res = new int[n][n][2];
ans = new int[n][n][2];
degree = new int[n][n][2];
for (int i = 0; i < n; ++i) {
for (int j = 1; j < n; ++j) {
Expand All @@ -26,39 +26,39 @@ public int catMouseGame(int[][] graph) {
}
Deque<int[]> q = new ArrayDeque<>();
for (int j = 1; j < n; ++j) {
res[0][j][MOUSE_TURN] = MOUSE_WIN;
res[0][j][CAT_TURN] = MOUSE_WIN;
ans[0][j][MOUSE_TURN] = MOUSE_WIN;
ans[0][j][CAT_TURN] = MOUSE_WIN;
q.offer(new int[] {0, j, MOUSE_TURN});
q.offer(new int[] {0, j, CAT_TURN});
}
for (int i = 1; i < n; ++i) {
res[i][i][MOUSE_TURN] = CAT_WIN;
res[i][i][CAT_TURN] = CAT_WIN;
ans[i][i][MOUSE_TURN] = CAT_WIN;
ans[i][i][CAT_TURN] = CAT_WIN;
q.offer(new int[] {i, i, MOUSE_TURN});
q.offer(new int[] {i, i, CAT_TURN});
}
while (!q.isEmpty()) {
int[] state = q.poll();
int t = res[state[0]][state[1]][state[2]];
int t = ans[state[0]][state[1]][state[2]];
List<int[]> prevStates = getPrevStates(state);
for (var prevState : prevStates) {
int pm = prevState[0], pc = prevState[1], pt = prevState[2];
if (res[pm][pc][pt] == TIE) {
if (ans[pm][pc][pt] == TIE) {
boolean win
= (t == MOUSE_WIN && pt == MOUSE_TURN) || (t == CAT_WIN && pt == CAT_TURN);
if (win) {
res[pm][pc][pt] = t;
ans[pm][pc][pt] = t;
q.offer(prevState);
} else {
if (--degree[pm][pc][pt] == 0) {
res[pm][pc][pt] = t;
ans[pm][pc][pt] = t;
q.offer(prevState);
}
}
}
}
}
return res[MOUSE_START][CAT_START][MOUSE_TURN];
return ans[MOUSE_START][CAT_START][MOUSE_TURN];
}

private List<int[]> getPrevStates(int[] state) {
Expand All @@ -78,4 +78,4 @@ private List<int[]> getPrevStates(int[] state) {
}
return pre;
}
}
}
16 changes: 8 additions & 8 deletions solution/0900-0999/0913.Cat and Mouse/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_prev_states(state):
return pre

n = len(graph)
res = [[[0, 0] for _ in range(n)] for _ in range(n)]
ans = [[[0, 0] for _ in range(n)] for _ in range(n)]
degree = [[[0, 0] for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(1, n):
Expand All @@ -29,28 +29,28 @@ def get_prev_states(state):
degree[i][j][CAT_TURN] -= 1
q = deque()
for j in range(1, n):
res[0][j][MOUSE_TURN] = res[0][j][CAT_TURN] = MOUSE_WIN
ans[0][j][MOUSE_TURN] = ans[0][j][CAT_TURN] = MOUSE_WIN
q.append((0, j, MOUSE_TURN))
q.append((0, j, CAT_TURN))
for i in range(1, n):
res[i][i][MOUSE_TURN] = res[i][i][CAT_TURN] = CAT_WIN
ans[i][i][MOUSE_TURN] = ans[i][i][CAT_TURN] = CAT_WIN
q.append((i, i, MOUSE_TURN))
q.append((i, i, CAT_TURN))
while q:
state = q.popleft()
t = res[state[0]][state[1]][state[2]]
t = ans[state[0]][state[1]][state[2]]
for prev_state in get_prev_states(state):
pm, pc, pt = prev_state
if res[pm][pc][pt] == TIE:
if ans[pm][pc][pt] == TIE:
win = (t == MOUSE_WIN and pt == MOUSE_TURN) or (
t == CAT_WIN and pt == CAT_TURN
)
if win:
res[pm][pc][pt] = t
ans[pm][pc][pt] = t
q.append(prev_state)
else:
degree[pm][pc][pt] -= 1
if degree[pm][pc][pt] == 0:
res[pm][pc][pt] = t
ans[pm][pc][pt] = t
q.append(prev_state)
return res[MOUSE_START][CAT_START][MOUSE_TURN]
return ans[MOUSE_START][CAT_START][MOUSE_TURN]
Loading