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

2 RRRLLRRRLR

0

 

5

2 RLLRRRLR

 


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

dp

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll; int N, S;
ll dp[][][];
char p[][][];
int mar[][];
bool vis[][];
ll ans = ; void f(int x, int y) {
vis[x][y] = ;
int v = x >= N ? : ;
if(mar[x][y] == -) return;
if(!vis[x + ][y - v]) {
f(x + ,y - v);
}
for(int i = ; i <= ; ++i) {
if(dp[x + ][y - v][i] != ) {
dp[x][y][ mar[x][y] + i] += dp[x + ][y - v][i];
p[x][y][mar[x][y] + i] = 'L';
}
} if(!vis[x + ][y + - v]) {
f(x + ,y + - v);
} for(int i = ; i <= ; ++i) {
if(dp[x + ][y + - v][i] != ) {
dp[x][y][ mar[x][y] + i] += dp[x + ][y + - v][i];
if(!p[x][y][mar[x][y] + i])
p[x][y][mar[x][y] + i] = 'R';
}
}
} void output(int x) {
printf("%d ", x - );
for(int i = , t = x,nowsum = S; i <= * N - ; ++i) {
printf("%c", p[i][t][nowsum]);
int v = i >= N ? : ;
if(p[i][t][nowsum] == 'L') {
nowsum -= mar[i][t];
t -= v;
} else {
nowsum -= mar[i][t];
t += - v;
}
}
} void solve() {
memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
memset(p, , sizeof(p)); ans = ;
for(int i = ; i <= N; ++i) {
dp[ * N - ][i][mar[ * N - ][i]] = ;
} int tar = ;
for(int i = N; i >= ; --i) {
f(, i);
if(dp[][i][S] != ) {
tar = i;
}
ans += dp[][i][S];
} printf("%lld\n", ans);
if(ans != ) {
output(tar);
} printf("\n");
}
int main()
{
freopen("sw.in", "r", stdin);
while(~scanf("%d%d", &N, &S) && (N + S)) {
memset(mar, -, sizeof(mar));
for(int i = ; i <= N; ++i) {
for(int j = i; j <= N; ++j) {
scanf("%d", &mar[i][j]);
}
} for(int i = N + ; i <= * N - ; ++i) {
for(int j = N - (i - N); j <= N; ++j) {
scanf("%d", &mar[i][j]);
}
}
solve();
}
return ;
}

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. 动态加载故事storyboard

    第一种方法: ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:@"Name o ...

  2. jQuery选项卡插件

    html结构 <ul id="tabs" class="tabs"> <li data-tab="users">Us ...

  3. Go defer延迟执行

    defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...

  4. iOS Bluetooth Reconnect

    蓝牙的重连主要分为以下两种: 1.恢复一些已知的设备,已知的设备就是在此次操作之前你扫描到的或者已经连接过的设备.用retrievePeripheralsWithIdentifiers:函数去完成回复 ...

  5. 【上传AppStore】iOS项目上传到AppStore步骤流程(第三章) - 基本信息总汇

    一.App ID(bundle identifier) App ID即Product ID,用于标识一个或者一组App. App ID应该和Xcode中的Bundle Identifier是一致(Ex ...

  6. vim替换及多行注释命令

    1.多行注释: . 进入命令行模式,按ctrl + v进入 visual block模式,然后按j, 或者k选中多行,把需要注释的行标记起来 . 按大写字母I,再插入注释符,例如// . 按esc键就 ...

  7. 20145120 《Java程序设计》第9周学习总结

    20145120 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC希望程序能操作所有数据库 操作JDBC驱动有4种类型: JDBC-ODBC Bridge Driver Na ...

  8. j2SE基回顾(一)

    1. 九种基本数据类型的大小,以及他们的封装类. 2. Switch能否用string做参数? 3. equals与==的区别. 4. Object有哪些公用方法? Object是所有类的父类,任何类 ...

  9. EasyUI 在aspx页面显示高度不正常解决办法

    <body class="easyui-layout"> <form id="form1" runat="server"& ...

  10. MySQL - 定时备份

    创建备份目录,在这里以/root/bak/mysql为例: cd mkdir bak cd bak mkdir mysql 在/usr/sbin下touch一个sh: cd /usr/sbin tou ...