UVA 10564 Paths through the Hourglass

Source: Internet
Author: User
Tags first row

Original question:
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 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 picture is described as ' 2 rrrllrrrlr '.

Given the values of each cell in a hourglass as well as a 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-integers N and S (2≤n≤20, 0≤s <), the number of cells in the Firs T 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 would contain N 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 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
9 |
2 9 3
1 0 4 4
4 8 7) 2 3
0 0
Sample Output
1
2 RRRLLRRRLR
0

5
2 RLLRRRLR

English:
From (Lucky cat)
There is a path in the left-hand image of an hourglass that is labeled. The path in the hourglass always starts at the top and ends at the bottom. When one of the columns in a column goes to the following, it can only go to the left or to the right. and the path value is the sum of the values of the lattice.

A path is composed of a number and a series of movements. Numbers represent the path from the top column to the beginning of the grid (the left-most lattice number is 0). Motion as one of R or L, which means to go down to the lower right or lower left. Take the path in the left image as an example: the path is 2 RRRLLRRRLR

Give you the value of each lattice in the hourglass, and a number s, please figure out how many paths are worth s. If there is more than one such path, the lowest starting position of the top column is given. If there is more than one, the dictionary is the smallest of the order.

Input

The first column of each group of tests has 2 integer n,s (2 <=n <=, 0 <= S < 500). n represents the number of the top column lattice in the Hourglass, and S represents the value of the required path. The next 2n-1 column describes the situation for each column in this hourglass. The numbers in each column are between 0 and 9, separated by a blank character. The columns in the hourglass are divided into squares of n, N-1, ..., 2, 1, 2, ..., N-1, N.

When n=s=0 represents the input end, the number of tests will not exceed 30. Please refer to sample Input.

Output

Please input 2 columns for each test. The first column is the number of the path, and the second is the representation of the required path. If the required path does not exist, the second column should be the blank column. Please refer to sample Output.

#include <bits/stdc++.h> using namespace std;
int n,s;
Long Long dp[501][41][41];
int hg[41][41];
    int main () {Ios::sync_with_stdio (false);
            while (Cin>>n>>s,n+s) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n-i+1;j++)
        cin>>hg[i][j];
        for (int i=n+1;i<2*n;i++) for (int j=1;j<=i-n+1;j++) cin>>hg[i][j];
        Memset (Dp,0,sizeof (DP));
        memset (Mark,0,sizeof (Mark));
        for (int i=1;i<=n;i++) dp[hg[2*n-1][i]][2*n-1][i]=1; for (int i=2*n-2;i>=n;i--) {for (int. j=1;j<=i-n+1;j++) {for (int k=hg I
                    [j];k<=s;k++) {dp[k][i][j]+=dp[k-hg[i][j]][i+1][j];
                DP[K][I][J]+=DP[K-HG[I][J]][I+1][J+1];
            }}} for (int i=n-1;i>=1;i--) {for (int j=1;j<=n-i+1;j++)
               { for (int k=hg[i][j];k<=s;k++) {dp[k][i][j]+=dp[k-hg[i][j]][i+1][j-1];
                DP[K][I][J]+=DP[K-HG[I][J]][I+1][J];
        }}} long long ans=0;
        int start=0;
            for (int i=1;i<=n;i++) {if (start==0&&dp[s][1][i]!=0) start=i;
        Ans+=dp[s][1][i];
        } cout<<ans<<endl;
            if (0==ans) {cout<<endl;
        Continue

            } else {cout<<start-1<< "";
                for (int i=1;i<n;i++) {S-=hg[i][start];
                    if (Dp[s][i+1][start-1]) {cout<< ' L ';
                start--;
            } else cout<< ' R ';
       } for (int i=n;i<2*n-1;i++) {S-=hg[i][start];         if (Dp[s][i+1][start]) cout<< ' L ';
                    else {cout<< ' R ';
                start++;
        }} cout<<endl;
}} return 0;
 }

Count from top to bottom (no path is counted)

#include <bits/stdc++.h> using namespace std;
int n,s;
Long Long dp[501][41][41];
int hg[41][41];
    int main () {Ios::sync_with_stdio (false);
            while (Cin>>n>>s,n+s) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n-i+1;j++)
        cin>>hg[i][j];
        for (int i=n+1;i<2*n;i++) for (int j=1;j<=i-n+1;j++) cin>>hg[i][j];
        Memset (Dp,0,sizeof (DP));
        memset (Mark,0,sizeof (Mark));
        for (int i=1;i<=n;i++) dp[hg[1][i]][1][i]=1; for (int i=1;i<=n;i++) {for (int. j=1;j<=n-i+1;j++) {for (int k=hg[i][
                    j];k<=s;k++) {dp[k][i][j]+=dp[k-hg[i][j]][i-1][j];
                DP[K][I][J]+=DP[K-HG[I][J]][I-1][J+1];
            }}} for (int i=n+1;i<2*n;i++) {for (int j=1;j<=i-n+1;j++) {for (int k=hg[i][j];k<=s;k++) {dp[k][i][j]+=dp[k-hg[i][j]][i-1][j];
                DP[K][I][J]+=DP[K-HG[I][J]][I-1][J-1];
        }}} long long ans=0;
        for (int i=1;i<=n;i++) {ans+=dp[s][2*n-1][i];

    } cout<<ans<<endl;
} return 0;









 }

Ideas:

Similar to the number of tower problems, due to the number of paths required, it is necessary to consider whether each number obtained after the acquisition can be satisfied and for a fixed s, so need to record s value, and similar to the knapsack problem. To the inverted triangle power, for a lattice, can be transferred from the upper left lattice, can also be transferred to the right of the lattice, so the state transfer equation is (from the top down to calculate hg[i][j] as the number of lattice)

Dp[s][i][j]+=dp[s-hg[i][j]][i-1][j]+dp[s-hg[i][j]][i-1][j+1] (inverted triangle)
Dp[s][i][j]+=dp[s-hg[i][j]][i-1][j]+dp[s-hg[i][j]][i-1][j-1] (the triangle being put)

However, this problem can not be calculated from the top, because the print path, so it needs to be deduced from the bottom of the roof.

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.