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
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
|
#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; }
uva 10564 Paths through the Hourglass (DP)
原文地址:http://blog.csdn.net/llx523113241/article/details/44245471