2014-08-10 23:47:20

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 Land 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 path exist, 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 0and 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
|
思路:骚气的一题,基础的计数DP QAQ,因为要输出路径且要字典序,所以要从沙漏底下往上递推,dp[i][j][k]表示到位置[i,j],路径和和k的方案数。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 using namespace std; 5 #define ll long long 6 7 int n,s; 8 ll dp[55][55][505]; 9 int v[55][55]; 10 11 void Print(int r,int c,int val){ 12 if(r == 2 * n - 1) 13 return; 14 if(r >= n){ 15 if(dp[r + 1][c][val - v[r][c]]){ 16 printf("L"); 17 Print(r + 1,c,val - v[r][c]); 18 } 19 else{ 20 printf("R"); 21 Print(r + 1,c + 1,val - v[r][c]); 22 } 23 } 24 else{ 25 if(dp[r + 1][c - 1][val - v[r][c]]){ 26 printf("L"); 27 Print(r + 1,c - 1,val - v[r][c]); 28 } 29 else{ 30 printf("R"); 31 Print(r + 1,c,val - v[r][c]); 32 } 33 } 34 } 35 36 int main(){ 37 //freopen("in.txt","r",stdin); 38 while(scanf("%d%d",&n,&s) == 2){ 39 if(n == 0 && s == 0) 40 break; 41 memset(v,-1,sizeof(v)); 42 memset(dp,0,sizeof(dp)); 43 for(int i = 1; i < n; ++i) 44 for(int j = 1; j <= n - i + 1; ++j) 45 scanf("%d",&v[i][j]); 46 for(int i = 1; i <= n; ++i) 47 for(int j = 1; j <= i; ++j) 48 scanf("%d",&v[i + n - 1][j]); 49 //init 50 for(int j = 1; j <= n; ++j) 51 dp[2 * n - 1][j][v[2 * n - 1][j]] = 1; 52 for(int i = 2 * n - 2; i >= n; --i){ 53 for(int j = 1; j <= n; ++j){ 54 if(v[i][j] == -1) 55 continue; 56 for(int k = v[i][j]; k <= s; ++k){ 57 dp[i][j][k] += (dp[i + 1][j][k - v[i][j]] + dp[i + 1][j + 1][k - v[i][j]]); 58 } 59 } 60 } 61 for(int i = n - 1; i >= 1; --i){ 62 for(int j = 1; j <= n; ++j){ 63 if(v[i][j] == -1) 64 continue; 65 for(int k = v[i][j]; k <= s; ++k){ 66 dp[i][j][k] += (dp[i + 1][j - 1][k - v[i][j]] + dp[i + 1][j][k - v[i][j]]); 67 } 68 } 69 } 70 ll sum = 0; 71 int pos = -1; 72 for(int j = 1; j <= n; ++j){ 73 if(pos == -1 && dp[1][j][s] != 0) 74 pos = j; 75 //printf("dp[%d][%d][%d] : %d\n",1,j,s,dp[1][j][s]); 76 sum += dp[1][j][s]; 77 } 78 printf("%lld\n",sum); 79 if(sum){ 80 printf("%d ",pos - 1); 81 Print(1,pos,s); 82 } 83 printf("\n"); 84 } 85 return 0; 86 }
本文介绍了一道经典的计数DP题目,通过从沙漏底部向上递归的方式解决路径求和问题。文章提供了完整的C++代码实现,并详细解释了如何记录路径及确保输出字典序最小的解决方案。
344

被折叠的 条评论
为什么被折叠?



