Problem F
Paths through the Hourglass
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds

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");
}
}