UVA 10564 Paths through the Hourglass (DP)

Source: Internet
Author: User

UVA 10564 Paths through the Hourglass


Paths through the Hourglass
Input:
standard input

Output: Standard Output

Time Limit: 2 Seconds

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 is directly below to the left or right of the cell in the path in the Prev IOUs row. The value of a path is the sum of the values in each of the cells in the path.

A path is described with an integer representing the starting point in the first row (the leftmost cell being0) F Ollowed 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 a hourglass as well as an integer S, calculate the number of distinct paths wit H 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-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 would containN integers, then N-1, ..., 2, 1, 2, ..., N-1, n.

The input would terminate with n=s=0. This case is should not being processed. There'll is less than , 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 InputOutput for Sample Input

6 41                                                                                           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP

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

2 8 7 2 5

3 6 0 2

1 3 4

2 5

3

7 2

2 9 3 /p>

1 0 4 4

4 8 7 2 3

0 0

   

1

2 RRRLLRRRLR

0

5

2 RLLRRRLR


The main idea: give an hourglass with numbers. Request to find out from the first layer at any point, go down, until the last layer, traversed the path and equal to the target value of the scheme number. The output scheme number, and the output of the scheme of the smallest dictionary sequence, if the number of scenarios is 0, then output a blank line. Problem Solving Ideas:1) If you read it directly, the DP will be a little bit troublesome to judge about it.because the upside is the opposite. So when you read it, you can handle it a little bit, and it will not be easy to make mistakes in direction judgment. 2)The DP[i][j][k] Array records the number of scenarios where the lattice (i, J) is added to the last row and K. 3) The DP array and the last record total scheme number of ans to use a long long. 4)DP starts with the last line, and the last line is all set to 1 first. 5) State transfer equation: dp[i][j][k]= dp[i + 1][j][k-v] + dp[i + 1][j + 1][k-v]; V is the value of the (I, j) lattice.



#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define N 55# Define M 505typedef 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] Array records the scheme number of squares (i, j) plus the last behavior 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)); MEM Set (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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.