uva 10564 Paths through the Hourglass (DP)

                   uva 10564 Paths through the Hourglass


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 being0) 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 as2 RRRLLRRRLR.

Given the values of each cell in an hourglass as well as an integer S, calculate the number of distinct paths with valueS. 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 integersN and S (2≤N≤20, 0≤S<500), the number of cells in the first row of the hourglass and the desired sum. Next follows2N-1 lines describing each row in the hourglass. Each line contains a space separated list of integers between0 and 9 inclusive. The first of these lines will containN 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 than30 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

 


题目大意:给出一个用数字组成的沙漏。要求求出从第一层任意一点开始,往下走,直到最后一层,走过的路径和等于目标值的方案数。输出方案数,并输出字典序最小的方案的具体走法,若方案数为0,则输出一个空行。
解题思路:
1)如果直接读入的话,dp时判断左右会稍微有点麻烦,因为上下会相反。所以读入的时候可以稍作处理,在方向判断上就不会容易出错。
2)dp[i][j][k]数组记录的是格子(i, j)加到最后一行和为k的方案数。
3)dp数组和最后记录总方案数的ans要用long long。
4)dp从最后一行开始,先将最后一行全部置为1。
5)状态转移方程:dp[i][j][k] = dp[i + 1][j][k - v] + dp[i + 1][j + 1][k - v];   v为(i, j)格子的值。



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define N 55
#define M 505
typedef long long ll;
using namespace std;
int n, m;
ll ans, dp[N][N][M];
int num[N * 2][N * 2];
void DP() { //dp[i][j][k]数组记录的是格子(i, j)加到最后一行为k的方案数
	for (int i = 2 * n - 2; i >= n; i--) {
		for (int j = i; j >= n - 1; j--) {
			if (i == 2 * n - 2) {
				dp[i][j][num[i][j]] = 1;
			}
			else {
				int w, v;
				w = v = num[i][j];
				for (; v <= m; v++) {
					dp[i][j][v] = dp[i + 1][j][v - w] + dp[i + 1][j + 1][v - w];
				}
			}
		}
	}
	int cnt = n - 1;
	for (int i = n - 1; i >= 0; i--) {
		for (int j = cnt; j <= n - 1; j++) {
			int w, v;
			w = v = num[i][j];
			for (; v <= m; v++) {
				dp[i][j][v] = dp[i + 1][j][v - w] + dp[i + 1][j + 1][v - w];
			}
			if (i == 0) {
				ans += dp[i][j][m];
			}
		}
		cnt--;
	}
}
void outPut() {
	printf("%lld\n", ans);
	int flag = 0;
	for (int i = 0; i < n; i++) {
		if (dp[0][i][m]) {
			flag = 1;
			printf("%d", i);
			int rec = i, s = m - num[0][i], flag2 = 1;
			for (int j = 1; j < 2 * n - 1; j++) {
				if (flag2) {
					printf(" ");
					flag2 = 0;
				}
				if (dp[j][rec][s]) {
					printf("L");
					s -= num[j][rec];
				}
				else if (dp[j][rec + 1][s]){
					printf("R");
					rec++;
					s -= num[j][rec];
				}
			}
			break;
		}
	}
	if (!flag) {
		printf("\n");
	}
	else printf("\n");
}
int main() {
	while (scanf("%d %d", &n, &m) == 2) {
		if (n == 0 && m == 0) break;
		memset(dp, 0, sizeof(dp));
		memset(num, -1, sizeof(num));
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				scanf("%d", &num[i][j]);
			}
		}	
		for (int i = n; i < 2 * n - 1; i++) {
			for (int j = 0; j < 2 + i - n; j++) {
				scanf("%d", &num[i][j + n - 1]);
			}
		}
		ans = 0;
		DP();
		outPut();
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值