Paths through the Hourglass+uva+dp

来源:互联网 发布:js上两个兄弟元素 编辑:程序博客网 时间:2024/04/26 17:47

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 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 0 and 9 inclusive. The first of these lines will contain N integers, thenN-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

 



解决方案:状态转移方程不难想到,但输出符合要求的路径就有点麻烦了,出发点位置要最靠前,路径的字典序要最小,开始我是顺着推的,然后再往回找路径,这就要一个dfs找出一个符合要求的路径,可是dfs会超时,所以后面看了人家的做法,原来还可以逆着推,然后再顺着找路径,这样一下子就找到符合条件的路径了。

对于上三角: dp[i][j][num+mat[i][j]]=dp[i+1][j][num]+dp[i+1][j-1][num];
对于下三角: dp[i][j][num+mat[i][j]]=dp[i+1][j][num]+dp[i+1][j+1][num];

至于路径可用一颗二叉树来存储

struct node
{
    int floor1,floor2;///左孩子1,右孩子2
    int room1,room2;
}

#include <iostream>#include<cstdio>#include<cstring>using namespace std;long long dp[60][60][505];int mat[60][60];char temp[100];int n,aim;struct node{    int floor1,floor2;    int room1,room2;} N[60][60][505];int start,sstart;int main(){    while(~scanf("%d%d",&n,&aim)&&(aim+n))    {        int level=1;        for(int k=0; k<505; k++)        {            for(int i=0; i<=2*n; i++)            {                for(int j=0; j<=n; j++)                {                    N[i][j][k].floor1=-1,N[i][j][k].room1=-1;                    N[i][j][k].floor2=-1,N[i][j][k].room2=-1;                }            }        }        for(int i=n; i>=2; i--)        {            for(int j=1; j<=i; j++)            {                scanf("%d",&mat[level][j]);            }            level++;        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<=i; j++)            {                scanf("%d",&mat[level][j]);            }            level++;        }        memset(dp,0,sizeof(dp));        for(int i=1; i<=n; i++)        {            dp[level-1][i][mat[level-1][i]]=1;        }        long long cnt=n-1;        for(int i=level-2; i>=1; i--)        {            for(int j=1; j<=cnt; j++)            {                for(int num=0; num<505; num++)                {                    if(i<n)                    {                        dp[i][j][num+mat[i][j]]+=dp[i+1][j][num];                        if(dp[i+1][j][num])                        {                            N[i][j][num+mat[i][j]].floor2=i+1;                            N[i][j][num+mat[i][j]].room2=j;                        }                        dp[i][j][num+mat[i][j]]+=dp[i+1][j-1][num];                        if(dp[i+1][j-1][num])                        {                            N[i][j][num+mat[i][j]].floor1=i+1;                            N[i][j][num+mat[i][j]].room1=j-1;                        }                    }                    else                    {                        dp[i][j][num+mat[i][j]]+=dp[i+1][j][num];                        if(dp[i+1][j][num])                        {                            N[i][j][num+mat[i][j]].floor1=i+1;                            N[i][j][num+mat[i][j]].room1=j;                        }                        dp[i][j][num+mat[i][j]]+=dp[i+1][j+1][num];                        if(dp[i+1][j+1][num])                        {                            N[i][j][num+mat[i][j]].floor2=i+1;                            N[i][j][num+mat[i][j]].room2=j+1;                        }                    }                }            }            if(i>n)            {                cnt--;            }            else cnt++;        }        cnt=0;        sstart=100;        for(int i=1; i<=n; i++)        {            cnt+=dp[1][i][aim];        }        for(int i=1; i<=n; i++)        {            if(dp[1][i][aim])            {                sstart=i;                int en=0;                int f=1,r=i,a=aim;                while(N[f][r][a].floor1!=-1||N[f][r][a].floor2!=-1)                {                    if(N[f][r][a].floor1!=-1)                    {                        int ff,rr,tt;                        tt=a-mat[f][r];                        ff=N[f][r][a].floor1;                        rr=N[f][r][a].room1;                        f=ff,r=rr,a=tt;                        temp[en++]='L';                    }                    else if(N[f][r][a].floor2!=-1)                    {                        int ff,rr,tt;                        tt=a-mat[f][r];                        ff=N[f][r][a].floor2;                        rr=N[f][r][a].room2;                        f=ff,r=rr,a=tt;                        temp[en++]='R';                    }                }                temp[en]='\0';                break;            }        }        printf("%lld\n",cnt);        if(cnt==0)        {            printf("\n");        }        else        {            printf("%d %s\n",sstart-1,temp);        }    }    return 0;}


0 0
原创粉丝点击