-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva910.cpp
80 lines (58 loc) · 1.16 KB
/
uva910.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
int N;
vector<vector<int> > graph;
bool winning_pos[30];
int memo[30][35];
int ways(int curent_pos, int moves_left)
{
if(memo[curent_pos][moves_left] != -1)
return memo[curent_pos][moves_left];
if(moves_left == 0)
{
if(winning_pos[curent_pos])
return 1;
else
return 0;
}
return memo[curent_pos][moves_left] = ways(graph[curent_pos][0], moves_left - 1)
+ ways(graph[curent_pos][1], moves_left - 1);
}
int main()
{
while(true)
{
cin >> N;
if(cin.eof())
break;
graph.clear();
graph.resize(N);
for(int pos = 0; pos < N; pos++)
{
char letter; cin >> letter;
char next_pos;
for(int i = 0; i <= 1; i++)
{
cin >> next_pos;
graph[pos].push_back(next_pos - 'A');
}
char special; cin >> special;
if(special == 'x')
winning_pos[pos] = true;
else
winning_pos[pos] = false;
}
int moves_left; cin >> moves_left;
memset(memo, -1, sizeof(memo));
int rez = ways(0, moves_left);
cout << rez << endl;
}
return 0;
}