forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
53 lines (51 loc) · 1.48 KB
/
Solution.cpp
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
class Solution {
public:
vector<vector<vector<int>>> memo;
vector<vector<int>> graph;
int catMouseGame(vector<vector<int>>& graph) {
int n = graph.size();
this->graph = graph;
memo.resize(n, vector<vector<int>>(n, vector<int>(2 * n + 10, -1)));
return dfs(1, 2, 0);
}
int dfs(int i, int j, int k) {
if (memo[i][j][k] != -1) return memo[i][j][k];
if (k >= 2 * graph.size()) memo[i][j][k] = 0;
else if (i == 0) memo[i][j][k] = 1;
else if (i == j) memo[i][j][k] = 2;
else if (k % 2)
{
bool tie = false, win = false;
for (int next : graph[j])
{
if (next == 0) continue;
int x = dfs(i, next, k + 1);
if (x == 2)
{
win = true;
memo[i][j][k] = 2;
break;
}
if (x == 0) tie = true;
}
if (!win) memo[i][j][k] = tie ? 0 : 1;
}
else
{
bool tie = false, win = false;
for (int next : graph[i])
{
int x = dfs(next, j, k + 1);
if (x == 1)
{
win = true;
memo[i][j][k] = 1;
break;
}
if (x == 0) tie = true;
}
if (!win) memo[i][j][k] = tie ? 0 : 2;
}
return memo[i][j][k];
}
};