题目:

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.


Expert as he was in this material, he saw at a glance that he'll
need a computer to calculate the number of ways to fill the large
rectangle whose dimensions were integer values, as well. Help him, so
that his dream won't turn into a nightmare!

Input

The
input contains several test cases. Each test case is made up of two
integer numbers: the height h and the width w of the large rectangle.
Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.

Output

For
each test case, output the number of different ways the given rectangle
can be filled with small rectangles of size 2 times 1. Assume the given
large rectangle is oriented, i.e. count symmetrical tilings multiple
times.

Sample Input

1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0

Sample Output

1
0
1
2
3
5
144
51205
题意很简单就是求用1*2的小木块,有几种方法能构成h*w的长方体。
相当蛋疼的题目,可能是我比较菜吧,想了好久才找到适合DP的状态,而且状态数太多了,把内存的给爆了,迫不得以,用预处理去掉一维,内存才够用
状态的表示:
b是当前dp的矩形的宽度
dp[h][state]
h代表当前高度
state是三进制数来表示当前高度上每列的状态
2代表与当前高度同高
1代表比当前高度矮一格
0代表比当前高度矮两格
比如当
b=4
h=2
三进制2222,2221
分别代表2*4的矩形,和缺了一个角的2*4矩形
状态的转移:
为了防止出现重复的计算的情况,我们要保证状态转移的唯一性。
我想到方法是,每次操作剩下图形最高的列中最右边的列,因为这个列是唯一的,所以可以保证的转移的唯一性。
我们对这个列操作有两种
1,去掉这个两格,即去掉高2宽1的小木块
2,若这个列左边相邻的列也是与其等高的列,去掉这两个列个一格,即去掉高1宽2的小木块
代码实现:
因为这个转移方程挺复杂的,写成递推比较麻烦,所以我写成了记忆话搜索
 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long dp[][],ans[][];
int u[],k[],b;
int check(int state)
{
int x=state,que[]= {},i;
for(i=; i<=; i++)
{
que[i]=x%;
x/=;
}
for(i=; i<=; i++)
{
if(que[i]==)
{
if(que[i+]==)
que[i+]=;
else
return ;
}
}
return ;
}
long long dfs(int h,int state)
{
if(dp[h][state]==-)
{
if(h==)
{
dp[h][state]=check(state);/**当只剩下一列时,检查这列是否能用高1宽2的小木块组成*/
}
else
{
int x,i;
x=state;
for(i=; i<=b; i++)/**寻找最右边且高度与h相等的那列*/
{
if(x%==)
{
break;
}
x/=;
}
if(i>b)
dp[h][state]=dfs(h-,state+u[b]);/**没有与h等高的列,所以h下降,扫描下一高度*/
else
{
dp[h][state]=dfs(h,state-*k[i]);/**去掉高2宽1的小木块*/
if(i<b&&(x/)%==)/**判断与x相邻列的是否也是与等高*/
{
dp[h][state]+=dfs(h,state-k[i]-k[i+]);/**去掉高1宽2的小木块*/
}
}
}
}
return dp[h][state];
}
int main()
{
int i,j,len;
u[]=;
k[]=;
for(i=; i<=; i++)
{
k[i]=k[i-]*;
u[i]=u[i-]*+;
}
for(i=; i<=; i++)/**i是宽度*/
{
len=q[i].size();
b=i;
memset(dp,-,sizeof(dp));
if(i%==)/**判断奇偶,因为若面积是奇数则坑定种类为零,不用算了*/
{
for(j=; j<=i; j+=)/**只算偶高度*/
{
ans[i][j]=dfs(j,*u[i]);
}
}
else
{
for(j=; j<=i; j++)/**j是高度*/
{
ans[i][j]=dfs(j,*u[i]);
}
}
}
while(scanf("%d%d",&i,&j)&&i)
{
if(i<j)
swap(i,j);
printf("%I64d\n",ans[i][j]);
}
return ;
}
												

最新文章

  1. c++并发练习---生产者消费者模型
  2. Mysql存储过程调用
  3. Ado.net中简单的DBHelper类(增删改查)
  4. Inline Workers--Web workers without a separate Javascript file
  5. 怎样将runlmbench 获取的数值传给上层app
  6. 使用python通过selenium模拟打开chrome窗口报错 出现 &quot;您使用的是不受支持的命令行标记:--ignore-certificate-errors
  7. UIAlertView(已经过时) UIActionView swift
  8. wdcp日志
  9. IPC进程间通信 - AIDL+Binder
  10. android性能优化优秀文章
  11. PAT (Advanced Level) 1085. Perfect Sequence (25)
  12. php与MySQL(php内置mysql函数)
  13. unix下各种查看“变量”的命令比较
  14. Linux学习历程——Centos 7 账户管理命令(用户组篇)groupadd groupmod groupdel
  15. 配置notepad++支持golang开发
  16. git checkout branch
  17. Linux网络编程学习(十一) ----- 五种I/O模式(第六章)
  18. MySQL之索引原理
  19. Python Redis hash
  20. Java服务CPU飙到99%问题排查

热门文章

  1. Eclipse:Win10中设置Courier New字体
  2. ReactiveCocoa概念解释篇
  3. PHPStorm+XDebug进行调试图文教程
  4. Voyager的安装及配置文件
  5. 在使用sql语句的一些注意事项(sql语句)
  6. python爬虫基础15-python图像处理,PIL库
  7. 请问batch_normalization做了normalization后为什么要变回来?
  8. 水题:UVa133-The Dole Queue
  9. 二叉排序树:HDU3791-二叉搜索树(用指针建立二叉排序树)
  10. 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录