Paths through the Hourglass - UVa 10564 dp

本文详细介绍了如何计算从倒置的小时玻璃的第一行到最后一行的路径,路径中每个单元格的值之和等于给定数的情况。通过递归算法和动态规划,文章解释了如何找到特定路径和的路径数量,以及如何输出开始点最小的路径,当有多条路径满足条件时,选择字典序最小的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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, 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

 



题意:从第一行到最后一行的路径权值和为给定的数值的情况有多少种,如果有多种解的时候输出开始点最小的路径,如果还是有多种解的话,输出字典序最小的路径。

思路:就是从下往上推,思路很简单,就是写起来比较麻烦。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
struct node
{ long long num[510][2];
}dp[45][45];
long long val[45][45];
int p[45],n,m;
void solve(int x1,int y1,int x2,int y2,long long pos)
{ int i,j,k;
  for(i=0;i<=m-val[x1][y1];i++)
   if(dp[x2][y2].num[i][0]>0)
   { dp[x1][y1].num[i+val[x1][y1]][0]+=dp[x2][y2].num[i][0];
     dp[x1][y1].num[i+val[x1][y1]][1]=pos;
   }
}
int main()
{ int i,j,k,x1,y1,x2,y2,ret,pos;
  long long ans;
  while(scanf("%d%d",&n,&m) && n+m)
  { p[1]=n;
    for(i=2;i<=n;i++)
    { p[i]=p[i-1]-1;
      p[i+n-1]=i;
    }
    for(i=1;i<=n*2-1;i++)
     for(j=1;j<=p[i];j++)
     { scanf("%lld",&val[i][j]);
       memset(dp[i][j].num,0,sizeof(dp[i][j].num));
     }
    for(i=1;i<=n;i++)
     dp[n*2-1][i].num[val[n*2-1][i]][0]=1;
    for(i=n*2-2;i>=n;i--)
     for(j=1;j<=p[i];j++)
     { solve(i,j,i+1,j+1,2);
       solve(i,j,i+1,j,1);
     }
    for(i=n-1;i>=1;i--)
    { solve(i,1,i+1,1,2);
      for(j=2;j<=p[i]-1;j++)
      { solve(i,j,i+1,j,2);
        solve(i,j,i+1,j-1,1);
      }
      solve(i,p[i],i+1,j-1,1);
    }
    ans=0;
    for(i=1;i<=n;i++)
     ans+=dp[1][i].num[m][0];
    if(ans==0)
    { printf("0\n\n");
      continue;
    }
    printf("%lld\n",ans);
    for(i=1;i<=n;i++)
     if(dp[1][i].num[m][0]>0)
      break;
    printf("%d ",i-1);
    x1=1;y1=i;ret=m;
    for(i=2;i<=n*2-1;i++)
    { if(dp[x1][y1].num[ret][1]==1)
      { ret-=val[x1][y1];
        printf("L");
        if(x1<n)
          y1--;
      }
      else
      { ret-=val[x1][y1];
        printf("R");
        if(x1>=n)
         y1++;
      }
      x1++;
    }
    printf("\n");
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值