UVa11420 - Chest of Drawers(动态规划)
来源:互联网 发布:linux安装rpm文件 编辑:程序博客网 时间:2024/06/07 01:38
Problem D
Chest of Drawers
Input: Standard Input
Output: Standard Output
Figure 1: A chest of drawers
A chest of drawers means awardrobe which has many drawers aligned vertically as shown in the figure onthe left. Although this is useful furniture, some problems arise when all thedrawers need have provisions of locking - that is sometimes a drawer is notsecured even if it is locked. For example assume that the third drawer from thetop is locked but the drawer immediately above it is not locked. Then thedrawer that is locked is also not secured because one can access it by pullingout the drawer immediately above it.
In a chest of n drawers, thereare a number of ways to ensure that exactly s drawers are secure. For examplefor the chest of drawers shown on the left, exactly four drawers can be securedin six ways. These six ways are shown in Figure 2.
Given the value of n and s, yourjob is to find out in how many ways they can be secured.
Figure 2: In this figure L means that the drawer is locked and U means that the corresponding drawer is unlocked. And here all six locking combinations are shown which ensures that exactly four drawers are secured. Letters corresponding the secured drawers are boldfaced.
Input
The input file contains at most5000 lines of inputs.
Each line contains two integers n and s (1 ≤ n ≤ 65 and 0 ≤ s ≤65). Here n is the total number of drawers and s is the number of drawers thatneeds to be secured.
Input is terminated by a linecontaining two negative numbers. This input should not be processed.
Output
For each line of input produceone line of output. This line contains an integer which denotes in how many, sdrawers out of the n drawers can be secured.
Sample Input Output for SampleInput
6 2
6 3
6 4
-1 -1
16
9
6
#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int N = 70;const int M = 2;LL memo[N][M][N];int n, s;bool input();void solve();LL dfs(int dep, int lock, int seq);int main(){#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);#endifwhile (input()) {solve();}return 0;}bool input(){scanf("%d%d", &n, &s);if (n < 0 && s < 0) return false;return true;}LL dfs(int dep, int lock, int seq){if (dep == n) return seq == s;LL& ans = memo[dep][lock][seq];if (ans != -1) return ans;ans = 0;if (!lock) {ans += dfs(dep + 1, 1, seq) + dfs(dep + 1, 0, seq); } else {ans += dfs(dep + 1, 1, seq + 1) + dfs(dep + 1, 0, seq);}return ans;}void solve(){memset(memo, -1, sizeof(memo));printf("%lld\n", dfs(1, 0, 0) + dfs(1, 1, 1));}
- UVa11420 - Chest of Drawers(动态规划)
- Game of Sum(动态规划)
- UVA 10891 Game of Sum (动态规划)
- lightoj 1147 - Tug of War 动态规划
- UVA437 The Tower of Babylon 动态规划
- [动态规划]UVA437 - The Tower of Babylon
- UVA437 - The Tower of Babylon - 动态规划
- POJ3122 - Sum of Different Primes - 动态规划
- HDU_4028_The time of a day_动态规划
- 【动态规划】:poj2567,Tug of War
- POJ1157_LITTLE SHOP OF FLOWERS_动态规划
- POJ LITTLE SHOP OF FLOWERS(动态规划)
- UVA10891[Game of Sum] 动态规划
- 动态规划!!!动态规划!!!
- POJ1157 LITTLE SHOP OF FLOWERS题解动态规划DP
- POJ3132Sum of Different Primes题解动态规划DP
- POJ 1157 LITTLE SHOP OF FLOWERS(动态规划)
- 【POJ1157】LITTLE SHOP OF FLOWERS (简单动态规划)
- C++读写EXCEL文件方式比较
- 监听端口号与PID
- pyqt 通过notepad打开中文文件
- Objective-C中的Category(分类)
- UVA 10790 How Many Points of Intersection? 简单数学题
- UVa11420 - Chest of Drawers(动态规划)
- CentOs怎么开机直接进入命令行模式
- 归并排序
- linux自动拷贝文件,并恢复ORACLE数据库
- C++ 虚函数的缺省参数问题
- php 使用 curl 发送 post 数据
- 数据结构与算法——线性时间排序(计数排序、基数排序、桶排序)
- 关于二分图的一些转载
- Spring 学习-------依赖注入