-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_LCP_07_numWays.cc
98 lines (92 loc) · 1.96 KB
/
Problem_LCP_07_numWays.cc
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
#include <queue>
#include <vector>
using namespace std;
class Solution
{
public:
// dfs
int numWays1(int n, vector<vector<int>>& relation, int k)
{
vector<vector<int>> edges(n);
for (auto& e : relation)
{
int from = e[0];
int to = e[1];
edges[from].push_back(to);
}
return f(0, 0, n, k, edges);
}
int f(int i, int step, int n, int k, vector<vector<int>>& edges)
{
if (step == k)
{
return i == n - 1 ? 1 : 0;
}
int ans = 0;
for (int to : edges[i])
{
ans += f(to, step + 1, n, k, edges);
}
return ans;
}
// bfs
int numWays2(int n, vector<vector<int>>& relation, int k)
{
vector<vector<int>> edges(n);
for (auto& e : relation)
{
int from = e[0];
int to = e[1];
edges[from].push_back(to);
}
int steps = 0;
queue<int> q;
q.push(0);
while (!q.empty() && steps < k)
{
steps++;
int size = q.size();
for (int i = 0; i < size; i++)
{
int index = q.front();
q.pop();
for (auto& next : edges[index])
{
q.push(next);
}
}
}
// 队列中剩下最后一轮所经过的节点
int ways = 0;
if (steps == k)
{
while (!q.empty())
{
if (q.front() == n - 1)
{
ways++;
}
q.pop();
}
}
return ways;
}
int numWays3(int n, vector<vector<int>>& relation, int k)
{
// dp[i][j] 含义为经过 i 轮传递到编号 j 的玩家的方案数
vector<vector<int>> dp(k + 1, vector<int>(n));
// 第1轮一定位于编号0玩家
dp[0][0] = 1;
for (int i = 0; i < k; i++)
{
for (auto& edge : relation)
{
int src = edge[0], dst = edge[1];
// 如果第 i 轮传递到编号 src 的玩家
// 则第 i+1 轮可以从编号 src 的玩家传递到编号 dst 的玩家
dp[i + 1][dst] += dp[i][src];
}
}
return dp[k][n - 1];
}
};