Problem F
Paths through the Hourglass
Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

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

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.  

1

2 RRRLLRRRLR

0

 

5

2 RLLRRRLR

 


Problemsetter: Jimmy Mårdell, Member of Elite Problemsetters' Panel

dp

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9.  
  10. int N, S;
  11. ll dp[][][];
  12. char p[][][];
  13. int mar[][];
  14. bool vis[][];
  15. ll ans = ;
  16.  
  17. void f(int x, int y) {
  18. vis[x][y] = ;
  19. int v = x >= N ? : ;
  20. if(mar[x][y] == -) return;
  21. if(!vis[x + ][y - v]) {
  22. f(x + ,y - v);
  23. }
  24. for(int i = ; i <= ; ++i) {
  25. if(dp[x + ][y - v][i] != ) {
  26. dp[x][y][ mar[x][y] + i] += dp[x + ][y - v][i];
  27. p[x][y][mar[x][y] + i] = 'L';
  28. }
  29. }
  30.  
  31. if(!vis[x + ][y + - v]) {
  32. f(x + ,y + - v);
  33. }
  34.  
  35. for(int i = ; i <= ; ++i) {
  36. if(dp[x + ][y + - v][i] != ) {
  37. dp[x][y][ mar[x][y] + i] += dp[x + ][y + - v][i];
  38. if(!p[x][y][mar[x][y] + i])
  39. p[x][y][mar[x][y] + i] = 'R';
  40. }
  41. }
  42. }
  43.  
  44. void output(int x) {
  45. printf("%d ", x - );
  46. for(int i = , t = x,nowsum = S; i <= * N - ; ++i) {
  47. printf("%c", p[i][t][nowsum]);
  48. int v = i >= N ? : ;
  49. if(p[i][t][nowsum] == 'L') {
  50. nowsum -= mar[i][t];
  51. t -= v;
  52. } else {
  53. nowsum -= mar[i][t];
  54. t += - v;
  55. }
  56. }
  57. }
  58.  
  59. void solve() {
  60. memset(vis, , sizeof(vis));
  61. memset(dp, , sizeof(dp));
  62. memset(p, , sizeof(p));
  63.  
  64. ans = ;
  65. for(int i = ; i <= N; ++i) {
  66. dp[ * N - ][i][mar[ * N - ][i]] = ;
  67. }
  68.  
  69. int tar = ;
  70. for(int i = N; i >= ; --i) {
  71. f(, i);
  72. if(dp[][i][S] != ) {
  73. tar = i;
  74. }
  75. ans += dp[][i][S];
  76. }
  77.  
  78. printf("%lld\n", ans);
  79. if(ans != ) {
  80. output(tar);
  81. }
  82.  
  83. printf("\n");
  84. }
  85. int main()
  86. {
  87. freopen("sw.in", "r", stdin);
  88. while(~scanf("%d%d", &N, &S) && (N + S)) {
  89. memset(mar, -, sizeof(mar));
  90. for(int i = ; i <= N; ++i) {
  91. for(int j = i; j <= N; ++j) {
  92. scanf("%d", &mar[i][j]);
  93. }
  94. }
  95.  
  96. for(int i = N + ; i <= * N - ; ++i) {
  97. for(int j = N - (i - N); j <= N; ++j) {
  98. scanf("%d", &mar[i][j]);
  99. }
  100. }
  101. solve();
  102. }
  103. return ;
  104. }

uva 10564的更多相关文章

  1. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  2. UVA 10564 十 Paths through the Hourglass

     Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  3. 01背包(类) UVA 10564 Paths through the Hourglass

    题目传送门 /* 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 状态转移方程:dp[i][j][k] = dp[i ...

  4. UVA 10564 - Paths through the Hourglass (dp)

    本文出自   http://blog.csdn.net/shuangde800 题目传送门 题意: 给一个相上面的图.要求从第一层走到最下面一层,只能往左下或右下走,经过的数字之和为sum. 问有多少 ...

  5. UVA 10564 Paths through the Hourglass(背包)

    为了方便打印路径,考虑从下往上转移.dp[i][j][S]表示在i行j列总和为S的方案, dp[i][j][S] = dp[i+1][left][S-x]+dp[i+1][right][S-x] 方案 ...

  6. UVa 10564 DP Paths through the Hourglass

    从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...

  7. UVA - 10564 Paths through the Hourglass

    传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...

  8. UVA 10564 计数DP

    也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...

  9. Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 5. Dynamic Programming

    10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=sho ...

随机推荐

  1. 《大话设计模式》ruby版代码:策略模式

    需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...

  2. o2o家庭助手demo

    前段时间跟一个同事出去游玩,在回来的大巴上面我们聊到了现在比较热门的o2o,说实话我自己早就想要在这个领域好好地玩一把.但是一直苦于没有很好地idea,再加上自己之前一直没有这方面的创业经验,所以一直 ...

  3. android 下滤镜效果的实现

    android 下滤镜效果的实现 滤镜过滤颜色已实现,简单版本可通过下面代码的3个参数实现黑白.红.绿...等7种过滤(RGB的7种组合). 理论上讲可以过滤为任意颜色.调整混合结果的比值就行了. p ...

  4. python djange输入中文错误的解决办法

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) ...

  5. Win8.1+vs2012+osg环境搭建

    Win8.1+vs2012+osg环境搭建 一.    相关准备 a) Osg源码 当前最新版:OpenSceneGraph-3.2.0.zip 下载链接: http://www.opensceneg ...

  6. apache maven

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj0AAAGXCAYAAABY/uEUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw ...

  7. M1事后分析报告(Postmortem Report)

    M1事后分析报告(Postmortem Report) 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们项目组所开发的软件为一个基于Andro ...

  8. 【转载】openldap 备份与导入 及相关问题--扩展

    http://www.cnblogs.com/ccdc/p/3356518.html 摘要: 对openldap进行备份时,直接使用slapcat命令进行备份,使用ldapadd还原出现问题及解决. ...

  9. Careercup - Microsoft面试题 - 5673934611546112

    2014-05-10 23:26 题目链接 原题: what is the best,worst and average case complexity for fibonacci no.s ..ex ...

  10. 三门概率问题之C#版

    前言: 早上看到一片关于三门问题的博客http://www.cnblogs.com/twocats/p/3440398.html,抱着该博客结论的怀疑态度用C#语言写了一些代码.实验证明该博客的结论是 ...