Problem F
Paths through the Hourglass
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the path (except the first) should be directly below to the left or right of the cell in the path in the previous row. The value of a path is the sum of the values in each cell in the path.
A path is described with an integer representing the starting point in the first row (the leftmost cell being 0) followed by a direction string containing the letters L and R, telling whether to go to the left or right. For instance, the path to the right is described as 2 RRRLLRRRLR.
Given the values of each cell in an hourglass as well as an integer S, calculate the number of distinct paths with value S. If at least one pathexist, you should also print the path with the lowest starting point. If several such paths exist, select the one which has the lexicographically smallest direction string.
Input
The input contains several cases. Each case starts with a line containing two integers N and S (2≤N≤20, 0≤S<500), the number of cells in the first row of the hourglass and the desired sum. Next follows 2N-1 lines describing each row in the hourglass. Each line contains a space separated list of integers between 0 and 9 inclusive. The first of these lines will contain N integers, then N-1, ..., 2, 1, 2, ..., N-1, N.
The input will terminate with N=S=0. This case should not be processed. There will be less than 30 cases in the input.
Output
For each case, first output the number of distinct paths. If at least one path exist, output on the next line the description of the path mentioned above. If no path exist, output a blank line instead.
Sample Input Output for Sample Input
6 41 6 7 2 3 6 8 1 8 0 7 1 2 6 5 7 3 1 0 7 6 8 8 8 6 5 3 9 5 9 5 6 4 4 1 3 2 6 9 4 3 8 2 7 3 1 2 3 5 5 26 2 8 7 2 5 3 6 0 2 1 3 4 2 5 3 7 2 2 9 3 1 0 4 4 4 8 7 2 3 0 0 |
1 2 RRRLLRRRLR 0
5 2 RLLRRRLR
|
Problemsetter: Jimmy Mårdell, Member of Elite Problemsetters' Panel
dp
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 typedef long long ll; 9 10 int N, S; 11 ll dp[50][50][505]; 12 char p[50][50][505]; 13 int mar[50][50]; 14 bool vis[50][50]; 15 ll ans = 0; 16 17 void f(int x, int y) { 18 vis[x][y] = 1; 19 int v = x >= N ? 1 : 0; 20 if(mar[x][y] == -1) return; 21 if(!vis[x + 1][y - v]) { 22 f(x + 1,y - v); 23 } 24 for(int i = 0; i <= 500; ++i) { 25 if(dp[x + 1][y - v][i] != 0) { 26 dp[x][y][ mar[x][y] + i] += dp[x + 1][y - v][i]; 27 p[x][y][mar[x][y] + i] = 'L'; 28 } 29 } 30 31 if(!vis[x + 1][y + 1 - v]) { 32 f(x + 1,y + 1 - v); 33 } 34 35 for(int i = 0; i <= 500; ++i) { 36 if(dp[x + 1][y + 1 - v][i] != 0) { 37 dp[x][y][ mar[x][y] + i] += dp[x + 1][y + 1 - v][i]; 38 if(!p[x][y][mar[x][y] + i]) 39 p[x][y][mar[x][y] + i] = 'R'; 40 } 41 } 42 } 43 44 void output(int x) { 45 printf("%d ", x - 1); 46 for(int i = 1, t = x,nowsum = S; i <= 2 * N - 2; ++i) { 47 printf("%c", p[i][t][nowsum]); 48 int v = i >= N ? 1 : 0; 49 if(p[i][t][nowsum] == 'L') { 50 nowsum -= mar[i][t]; 51 t -= v; 52 } else { 53 nowsum -= mar[i][t]; 54 t += 1 - v; 55 } 56 } 57 } 58 59 void solve() { 60 memset(vis, 0, sizeof(vis)); 61 memset(dp, 0, sizeof(dp)); 62 memset(p, 0, sizeof(p)); 63 64 ans = 0; 65 for(int i = 1; i <= N; ++i) { 66 dp[2 * N - 1][i][mar[2 * N - 1][i]] = 1; 67 } 68 69 int tar = 0; 70 for(int i = N; i >= 1; --i) { 71 f(1, i); 72 if(dp[1][i][S] != 0) { 73 tar = i; 74 } 75 ans += dp[1][i][S]; 76 } 77 78 printf("%lld ", ans); 79 if(ans != 0) { 80 output(tar); 81 } 82 83 printf(" "); 84 } 85 int main() 86 { 87 freopen("sw.in", "r", stdin); 88 while(~scanf("%d%d", &N, &S) && (N + S)) { 89 memset(mar, -1, sizeof(mar)); 90 for(int i = 1; i <= N; ++i) { 91 for(int j = i; j <= N; ++j) { 92 scanf("%d", &mar[i][j]); 93 } 94 } 95 96 for(int i = N + 1; i <= 2 * N - 1; ++i) { 97 for(int j = N - (i - N); j <= N; ++j) { 98 scanf("%d", &mar[i][j]); 99 } 100 } 101 solve(); 102 } 103 return 0; 104 }