Problem F
Paths through the Hourglass
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
![](http://uva.onlinejudge.org/external/105/p10564.gif)
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
|
题意:给定一个类似沙漏的东西。求出从顶到底,路径和为k的种数,且输出其中一种的起点和路径。
思路:找种数dp即可。i,j表示格子,k为和,从底往顶,当前格子的种数等于它与下一行的左右两个满足和关系的格子之和。
代码:
#include <stdio.h>
#include <string.h>
int n, N, k, map[45][45];
long long dp[45][45][505];
int main() {
while (~scanf("%d%d", &N, &k) && N || k) {
n = 2 * N - 1;
for (int i = 1; i < N; i ++)
for (int j = 1; j <= N - i + 1; j ++) {
scanf("%d", &map[i][j]);
}
for (int i = N; i <= n; i ++)
for (int j = 1; j <= i - N + 1; j ++) {
scanf("%d", &map[i][j]);
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= N; i ++) {
dp[n][i][map[n][i]] = 1;
}
for (int i = n - 1; i >= N; i --) {
for (int j = 1; j <= i - N + 1; j ++) {
for (int kk = map[i][j]; kk <= k; kk ++) {
dp[i][j][kk] += dp[i + 1][j][kk - map[i][j]] + dp[i + 1][j + 1][kk - map[i][j]];
}
}
}
for (int i = N - 1; i >= 1; i --) {
for (int j = 1; j <= N - i + 1; j ++) {
for (int kk = map[i][j]; kk <= k; kk ++) {
dp[i][j][kk] += dp[i + 1][j - 1][kk - map[i][j]] + dp[i + 1][j][kk - map[i][j]];
}
}
}
long long ans = 0;
for (int i = 1; i <= N; i ++) {
ans += dp[1][i][k];
}
printf("%lld\n", ans);
if (ans == 0)
printf("\n");
for (int i = 1; i <= N; i ++) {
if (dp[1][i][k]) {
printf("%d ", i - 1);
for (int j = 2; j <= N; j ++) {
if (dp[j][i - 1][k - map[j - 1][i]]) {
printf("L");
k -= map[j - 1][i];
i --;
}
else if (dp[j][i][k - map[j - 1][i]]) {
printf("R");
k -= map[j - 1][i];
}
}
for (int j = N + 1; j <= n; j ++) {
if (dp[j][i][k - map[j - 1][i]]) {
printf("L");
k -= map[j - 1][i];
}
else if (dp[j][i + 1][k - map[j - 1][i]]) {
printf("R");
k -= map[j - 1][i];
i ++;
}
}
printf("\n");
break;
}
}
}
return 0;
}