题目链接http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1505
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
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][k] i ,j代表行列,k代表s值,dp是取了这个点之后的状态
因为要找字典序最小的,所以从下往上dp,这样用个tag标记每个格子是否可以从左或右走上来
然后因为最后ans可能很大,所以要用long long
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n,s;
int num[50][25];
long long dp[50][50][550];
int tag[50][50][550][2];
int main(){
while(1){
memset(tag,0,sizeof(tag));
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&s);
if(n == 0 && s == 0) break;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n-i+1;j++)
scanf("%d",&num[i][j]);
}
for(int i = n+1;i <= 2*n-1;i++){
for(int j = 1;j <= i-n+1;j++)
scanf("%d",&num[i][j]);
}
for(int i = 1;i <= n;i++){
int tem = s - num[2*n-1][i];
dp[2*n-1][i][tem] = 1;
}
for(int i = 2*n-2;i >= n;i--){
for(int j = 1;j <= i-n+1;j++){
for(int k = 0;k <= s;k++){
int last = k+num[i][j];
if(dp[i+1][j][last] != 0){
dp[i][j][k] += dp[i+1][j][last];
tag[i][j][k][0] = 1;
}
if(dp[i+1][j+1][last] != 0){
dp[i][j][k] += dp[i+1][j+1][last];
tag[i][j][k][1] = 1;
}
}
}
}
for(int i = n-1;i >= 1;i--){
for(int j = 1;j <= n-i+1;j++){
for(int k = 0;k <= s;k++){
int last = k + num[i][j];
if(dp[i+1][j-1][last] != 0){
dp[i][j][k] += dp[i+1][j-1][last];
tag[i][j][k][0] = 1;
}
if(dp[i+1][j][last] != 0){
dp[i][j][k] += dp[i+1][j][last];
tag[i][j][k][1] = 1;
}
}
}
}
int ansx = -1;
long long ans = 0;
for(int i = 1;i <= n;i++){
if(dp[1][i][0] != 0 && ansx == -1){
ansx = i;
}
ans += dp[1][i][0];
}
printf("%lld\n",ans);
if(ans == 0){
cout << endl;
continue;
}
printf("%d ",ansx-1);
int j = ansx,now = 0;
for(int i = 1;i < 2*n-1;i++){//cout << num[i][j];
int p = j;
if(tag[i][j][now][0] != 0){
cout << 'L';
if(i < n)
j--;
}
else{
cout << 'R';
if(i >= n)
j++;
}
now += num[i][p];
}
cout << endl;
}
return 0;
}