-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 10564 - Paths through the Hourglass.cpp
84 lines (71 loc) · 1.78 KB
/
UVA 10564 - Paths through the Hourglass.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
#define vii vector<vector<int> >
#define inf 1000000000
string path, ans;
long long memo[40][20][500];
int n, s;
vii in;
long long dp(int i, int j, int sum)
{
if(i == 2 * n - 2){
if(j < 0 || j == in[i].size()) return 0;
if(sum + in[i][j] == s) {
if(ans.length() == 0) ans = path;
return 1;
}
return 0;
}
if(j < 0 || j == in[i].size()) return 0;
long long &ret = memo[i][j][sum];
if(ret != -1) return ret;
ret = 0;
if(i < n - 1){
path.push_back('L');
ret += dp(i + 1, j -1 , sum + in[i][j]);
path.pop_back();
path.push_back('R');
ret += dp(i + 1, j, sum + in[i][j]);
path.pop_back();
}else{
path.push_back('L');
ret += dp(i + 1, j, sum + in[i][j]);
path.pop_back();
path.push_back('R');
ret += dp(i + 1, j + 1, sum + in[i][j]);
path.pop_back();
}
return ret;
}
int main()
{
while(1){
scanf("%d%d", &n, &s);
if(!n && !s) return 0;
ans.clear();
in.assign(2 * n - 1, vi(0) );
int p = n, x = -1;
for(int i=0; i<2 * n - 1; i++){
in[i].assign(p, 0);
for(int j = 0; j<p; j++){
scanf("%d", &in[i][j]);
}
p += x;
if(!p){
x = 1;
p = 2;
}
}
memset(memo, -1, sizeof memo);
long long t = 0;
int start = -1;
for(int i=0; i<n; i++){
t += dp(0, i, 0);
if(start == -1 && ans.size()) start = i;
}
if(ans.size()){
printf("%lld\n", t);
cout<<start<<" "<<ans<<endl;
}else printf("0\n\n");
}
}