uva 10564 - Paths through the Hourglass

来源:互联网 发布:labview 算编程吗 编辑:程序博客网 时间:2024/06/07 03:51

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

 


这道题和数字三角形没有本质区别,无非多了一维记录状态,再稍微加了一点读入数据的难度。

dp[i][j][s]=dp[i+1][j][s-mat[i][j]]+dp[i+1][j+1][s-mat[i][j]];


代码:

#include<cstdio>#include<iostream>#include<cstring>#define Maxn 50using namespace std;int n,mat[Maxn][Maxn];long long dp[Maxn][Maxn][510];void print(int i,int j,int s){    if(i==(n<<1)-1) return;    if(dp[i+1][j][s-mat[i][j]]){        printf("L");        print(i+1,j,s-mat[i][j]);    }    else{        printf("R");        print(i+1,j+1,s-mat[i][j]);    }}int main(){    int s;    while(scanf("%d%d",&n,&s),n){        for(int i=1;i<=n;i++)            for(int j=i;j<=n;j++)                scanf("%d",&mat[i][j]);        for(int i=n+1;i<n<<1;i++)            for(int j=n;j<=i;j++)                scanf("%d",&mat[i][j]);        memset(dp,0,sizeof dp);        int r=(n<<1)-1;        for(int i=n;i<=r;i++)            dp[r][i][mat[r][i]]=1;        for(int i=r-1;i>n;i--)            for(int j=n;j<=i;j++)                for(int v=mat[i][j],k=mat[i][j];k<=s;k++)                    dp[i][j][k]=dp[i+1][j][k-v]+dp[i+1][j+1][k-v];        for(int i=n;i>0;i--)            for(int j=i;j<=n;j++)                for(int v=mat[i][j],k=mat[i][j];k<=s;k++)                    dp[i][j][k]=dp[i+1][j][k-v]+dp[i+1][j+1][k-v];        long long ans=0;        for(int i=1;i<=n;i++) ans+=dp[1][i][s];        printf("%lld\n",ans);        for(int i=1;i<=n;i++){            if(dp[1][i][s]){                printf("%d ",i-1);                print(1,i,s);                break;            }        }        puts("");    }return 0;}


0 0
原创粉丝点击