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]

方案O(2^2*n-1),结果要用long long保存。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 22,maxs = 501;
int hg[maxn<<1][maxn];
ll dp[maxn<<1][maxn][maxs];

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int n, S;
    while(scanf("%d%d", &n, &S) ,n){
        int rc = 0;
        for(int i = n; i > 1; i--){
            int *h = hg[rc++];
            for(int j = 1; j <= i; j++){
                scanf("%d",h+j);
            }
        }
        for(int i = 1; i <= n; i++){
            int *h = hg[rc++];
            for(int j = 1; j <= i; j++){
                scanf("%d",h+j);
            }
        }
        rc--;
        memset(dp,0,sizeof(dp));
        for(int j = 1; j <= n; j++){
            dp[rc][j][hg[rc][j]] = 1;
        }
        while(rc>=n){
            for(int j = 1, mj = (rc-- + 1) - n; j <= mj; j++){
                int x = hg[rc][j];
                ll *f = dp[rc][j];
                for(int s = S-x; s >= 0; s--){
                    f[s+x] = dp[rc+1][j][s]+dp[rc+1][j+1][s];
                }
            }
        }
        for(int i = 2; rc--,i <= n; i++){
            for(int j = 1; j <= i; j++){
                int x = hg[rc][j];
                ll *f = dp[rc][j];
                for(int s = S-x; s >= 0; s--){
                    f[s+x] = dp[rc+1][j-1][s]+dp[rc+1][j][s];
                }
            }
        }
        ll ans = 0;
        int index = 0;
        for(int j = 1; j <= n; j++){
            if(dp[0][j][S]){
                ans += dp[0][j][S];
                if(!index) index = j;
            }
        }
        printf("%lld\n",ans);
        if(ans){
            int j = index, s = S ;
            printf("%d ",j-1);
            rc = n;
            for(int i = 1; i < rc; i++){
                s -= hg[i-1][j];
                if(dp[i][j-1][s]){
                    putchar(‘L‘);
                    j--;
                }else {
                    putchar(‘R‘);
                }

            }
            rc = 2*n-1;
            for(int i = n; i < rc; i++){
                s -= hg[i-1][j];
                if(dp[i][j][s]){
                    putchar(‘L‘);
                }else {
                    j++;
                    putchar(‘R‘);
                }
            }
        }
        puts("");
    }
    return 0;
}
时间: 2024-12-23 20:42:39

UVA 10564 Paths through the Hourglass的相关文章

UVA 10564 Paths through the Hourglass[DP 打印]

UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走 沙茶的忘注释掉文件WA好多次 #include <iostream> #include <cstdio> #include <algor

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass 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

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

题目传送门 1 /* 2 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 3 状态转移方程:dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似 4 因为要输出字典序最小的,打印路径时先考虑L 5 */ 6 /************************************************ 7 * Author :Runni

UVa 10564 - Paths through the Hourglass(DP)

Description 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 pat

UVA 10564 十 Paths through the Hourglass

Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10564 1 #include <stdio.h> 2 #include <string.h> 3 4 int n,s; 5 long long dp[55][56][550]; 6 int a[56][55]; 7 8 i

uva10564 - Paths through the Hourglass(递推)

题目:uva10564 - Paths through the Hourglass(递推) 题目大意:给出这样的两个数塔,然后给出一个值,问你能否从这个数塔中找到路径,路径上的值之和等于这个数,输出这样的路径的总数,如果多条打印路径先挑开始的位置(0..n - 1)最小的,如果这样还是有多条,在比较后面的向左向右字典序最小的. 解题思路:一开始两个数塔一个正着推,一个倒着推,结果数目是出来了,但是路径就难办了,最后把这两个数塔都正着推,只是状态转移方程不一样.这样路径输出就比较好处理. 代码:

uva 10564

Problem FPaths through the HourglassInput: 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

UVa 10564 DP Paths through the Hourglass

从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int n, sum; 7 int a[50][25]; 8 long long d[50][50][550]; 9 int p[50][50][550][2];

UVA10564 - Paths through the Hourglass

1 /* 2 题意: 3 输入N,S,沙漏型的 2*N-1行数据,问从上走到下和为 S 的方法有多少种, 4 每个格子只能走到它下面相邻的两个格子 . 5 第一行输出多少种方式 ,第二行输出 起点下标 与 路径 . 6 7 还是看了题解之后才造怎么写,鲜有不看就会的啊= =,还需努力! 8 dp[i][j][k] 表示走到 (i,j)时和为 K 的方法有多少种. 9 从下面走到上面,好确定起点. 10 用一个数组记录方向,0->L, 1->R 11 12 */ 13 14 15 #includ