算法题:UVA 10564 Paths through the Hourglass(dp)

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 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 pathexist, 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

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

时间: 2024-08-27 11:17:49

算法题:UVA 10564 Paths through the Hourglass(dp)的相关文章

算法:uva 10934 Dropping water balloons(dp | 难想)

题意 你有k个一模一样的水球,在一个n层楼的建筑物上进行测试,你想知道水球最低从几层楼往 下丢可以让水球破掉.由于你很懒,所以你想要丢最少次水球来测出水球刚好破掉的最低楼层.(在最糟情 况下,水球在顶楼也不会破)你可以在某一层楼丢下水球来测试,如果水球没破,你可以再捡起来继续用. Input 输入的每一行包含多组测试,每组测试为一行.每组测试包含两个整数 k 和 n, 1 <= k <= 100 而 n 是一个 64 位的整数(没错,这栋建筑物的确很高),最后一组k = 0,n=0 代表 结束

算法:uva 1351 String Compression(字符串区间dp)

题目大意 给一个字符串,可以把连续相同的部分进行缩写成k(S)的形式,S是一个字符串,k表示 有连续相同的S 例如,abgogogogo,可以缩写成ab4(go). 还可以嵌套缩写,比如 "nowletsgogogoletsgogogo", 缩写成"now2(lets3(go))" 思路 一道区间dp,但是这题并 不好想 f(i, j)表示字符串的i~j位的最小位数 那么 f(i, j) = min{  min{ f(i,k)+f(k+1, j), i<=k&

算法:uva 10003 Cutting Sticks (区间dp)

题目大意 一根长为l的木棍,上面有n个"切点",每个点的位置为c[i] 要按照一定顺 序把每个点都砍段,最后变成了n+1段 每砍一次,就会有一个花费,例如长度为10,"切点"为2,那么砍完 后会变成两段2,8, 那么花费为2+8=10 如果有多个"切点",那么不同顺序切会得到不同的花费. 最小 花费是多少? 思路 注意要增加一个c[n] = l f(i, j) 表示(i,j)区间的最小花费 f(i, j) = min{ f(i,k)+f(k+1,

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:uva 10318

题目链接: 首先,可以确定每个格子只能选一次,因为选任何大于0的偶数次,等于没有效果 一样. 然后,就可以把这题理解成从r*c的矩阵中选择一些格子进行"点亮"操作,使得最终所 有格子都是"亮"的状态.那么,每个格子要么有点亮操作,要么没有,总共复杂度为2^25,显然必须 进行减枝. 假设从第一行第一列开始,从左往右,从上往下一次依次选择,对于当前所在位置( x, y),它已经不能影响到x-2以前的行数了,所以当到x行时,如果第x-2行没有全部点亮,则进行减枝 . 此

算法题:uva 1330

题目链接: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 以前做过一道一维的,这题只是变成了二维的,其他方法都一样.HDU 1506  Largest Rectangle in a Histogram   题解 代码1: #include<cstdio> #include<cs

一个算法题,求答案啊啊啊啊

问题描述 一个算法题,求答案啊啊啊啊 白班 09:00-18:00 通班 09:00-21:00 每个人每个月通班数量必须等于早中班和中晚班数量之和 早中班 09:00-15:00 中晚班 15:00-21:00 假设:每月按照30计算. 排班规则: 1.每个人每个月固定休息6天连续上班天数不超过7天. 2.每天各班次上班的人数最低需求:8个白班5个通班1个早中班,2个中晚班. 3.每个月每个人的通班天数安排不超过8天. 4.每个人每个月早中班和中晚班的天数之和需要与通班天数相等. 5.每月最多

算法:uva 10817

题目大意 某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资 使得每门课都至少有两名教师教学.在职教师必须招聘. 思路 这题不太好想,搞了很久.. f[s1][s2]: s1表示课程集合{ s1 }都至少有一个教师教的情况. s2表示课程集合{ s2 }都至少有两个教师教的情况. 每个求职者的pi, 对于每个求职者,要 么选,要么不选,就是01背包问题. 对于s1,s2,可以根据当前枚举到的求职者课程即可,可推出下 一个状态: nextS1 = p[i] | s1,

算法题:把阿拉伯金额转化为汉字表示的金额

n年没写算法题了,今天用了20分钟写了一个,要求如题,感觉算法有所退步,老了 using System; using System.Text; namespace money { class Program { static void Main(string[] args) { StringBuilder sb=new StringBuilder(); var strValue = Console.ReadLine(); var strlist = strValue.Split('.'); if