Algorithm title: UVA 10564 Paths through the Hourglass (DP)

Source: Internet
Author: User

In the hourglass to the right a path is marked. A path always starts at the "the" and ends at the last row. Each cell in the path (except the "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 the path.

A path is described with an integer representing the starting point in the "the leftmost cell being 0) followed by a direction string containing the letters L and R, telling whether to go to the left or righ T. For instance, the ' path to the ' right ' is described as 2 rrrllrrrlr.

Given the values of each cell in a hourglass as as as as as as an integer S, calculate the number of distinct paths wit H value S. If at least one pathexist, your should also print the path with the lowest starting point. If Several such paths exist, select the one which has the lexicographically smallest the 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 I n the 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 A 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 should is processed. There is less than cases in the input.

Output

For each case, the number of distinct paths. If at least one path exist, output in 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

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.