传送门:https://vjudge.net/problem/UVA-10564

题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前的方案,以及此方案的起点编号,起点相同则字典序最小。

题解:

  很容易想到一个DP,dp[i][j][S]代表到第i层,第j列,从第一层到这里的路径和为S的方案数,最后只要查询最后一行的方案数即可了。但是这样很不好输出方案!我们可能需要从终点向上dfs,但是又无法保证起点编号最小以及字典序最小。但是我们能从终点向上dfs,那么如果我们反过来DP呢?可行啊,这样就可以从起点向终点dfs了,贪心选择可行的路径即可。
  需要注意的是上下两个一半的沙漏的转移一个是到j与j+1,一个是j与j-1,要考虑清楚细节!

  1. #include<queue>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<cstring>
  5. #include<iostream>
  6. #include<algorithm>
  7. #define RG register
  8. #define LL long long
  9. #define fre(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
  10. using namespace std;
  11. const int MAXN=,MAXS=;
  12. int n;
  13. LL ans,S;
  14. LL a[MAXN*][MAXN],dp[MAXN*][MAXN][MAXS];
  15. void dfs(int x,int y,int sum);
  16. int main()
  17. {
  18. while(scanf("%d%lld",&n,&S)!=EOF)
  19. {
  20. if(n+S==)break;
  21. for(int i=;i<=n;i++) for(int j=;j<=n-i+;j++) scanf("%lld",&a[i][j]);
  22. for(int i=n+;i<*n;i++) for(int j=;j<=i-n+;j++) scanf("%lld",&a[i][j]);
  23. memset(dp,,sizeof dp);
  24. for(int i=;i<=n;i++) dp[n*-][i][a[n*-][i]]=;
  25. for(int i=n*-;i>=n;i--)
  26. for(int j=;j<=i-n+;j++)
  27. for(int k=;k<=S;k++)
  28. {
  29. if(dp[i+][j][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j][k];
  30. if(dp[i+][j+][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j+][k];
  31. }
  32. for(int i=n-;i>=;i--)
  33. for(int j=;j<=n-i+;j++)
  34. for(int k=;k<=S;k++)
  35. {
  36. if(dp[i+][j][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j][k];
  37. if(dp[i+][j-][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j-][k];
  38. }
  39. ans=; for(int i=;i<=n;i++) ans+=dp[][i][S]; printf("%lld\n",ans);
  40. for(int i=;i<=n;i++)
  41. if(dp[][i][S])
  42. {
  43. printf("%d ",i-);
  44. int x=,y=i,sum=S;
  45. while(x<n*-)
  46. {
  47. if(x<n)
  48. {
  49. if(dp[x+][y-][sum-a[x][y]]) sum-=a[x][y],y--,printf("L");
  50. else sum-=a[x][y],printf("R");
  51. }
  52. else
  53. {
  54. if(dp[x+][y][sum-a[x][y]]) sum-=a[x][y],printf("L");
  55. else sum-=a[x][y],y++,printf("R");
  56. }
  57. x++;
  58. }
  59. break;
  60. }
  61. printf("\n");
  62. }
  63. return ;
  64. }

UVA - 10564 Paths through the Hourglass的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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] 方案 ...

  5. UVA 10564_ Paths through the Hourglass

    题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...

  6. UVA 10564 十 Paths through the Hourglass

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

  7. uva 10564

    Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...

  8. UVa 10564 DP Paths through the Hourglass

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

  9. UVA 10564 计数DP

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

随机推荐

  1. POJ3067:Japan(线段树)

    Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for th ...

  2. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 使用Napa开发SharePoint应用程序

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 使用Napa开发SharePoint应用程序         假设 ...

  3. SQL模糊查询碰到空值怎么办?

    作者:iamlaosong SQL查询语句用%来做模糊查询.程序中一般要求用户输入部分信息,依据这个信息进行模糊查询. 比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全 ...

  4. HDU 1312 Red and Black 第一题搜索!

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. nyoj 题目10 skiing —— 南阳oj

    题目信息例如以下: skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区 ...

  6. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

  7. How MySQL Opens and Closes Tables refuse connections 拒绝连接的原因 file descriptors

    MySQL :: MySQL 5.7 Reference Manual :: 8.4.3.1 How MySQL Opens and Closes Tables https://dev.mysql.c ...

  8. 在Android Studio中移除导入的模块依赖

    进入settings.gradle(Project Settings) include ':app', ':pull_down_list_view' 要移除的Module dependency为“pu ...

  9. Ural 1635 Mnemonics and Palindromes(DP)

    题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...

  10. 百度dureos CMake Error

    CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, ...