题目:

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 ;
}
												

POJ 2411Mondriaan's Dream的更多相关文章

  1. POJ:2411-Mondriaan's Dream(矩形拼接方案)

    题目链接:http://poj.org/problem?id=2411 解题心得: 可以说是很经典的一个状压dp了,写dfs遍历肯定是要超时的,这个题的状态转移方程对新手来说有点吃力. 状态转移用的是 ...

  2. POJ 2411_Mondriaan's Dream

    题意: 用1*2和2*1的方块将给定长宽的矩形填满.问有多少种放法,对称的算两种. 分析: 状态压缩dp 首先用0表示前一行没有竖块占用这个位置,而1表示该位置和他上方的位置放了一个竖块,从而压缩状态 ...

  3. poj 2411 Mondriaan&#39;s Dream 【dp】

    题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...

  4. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  5. [poj P2411] Mondriaan's Dream

    [poj P2411] Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18023   A ...

  6. POJ 2411 Mondriaan's Dream 插头dp

    题目链接: http://poj.org/problem?id=2411 Mondriaan's Dream Time Limit: 3000MSMemory Limit: 65536K 问题描述 S ...

  7. Poj 2411 Mondriaan's Dream(压缩矩阵DP)

    一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...

  8. Mondriaan's Dream POJ - 2411

    Mondriaan's Dream POJ - 2411 可以用状压dp,但是要打一下表.暴力枚举行.这一行的状态.上一行的状态,判断如果上一行的状态能转移到这一行的状态就转移. 状态定义:ans[i ...

  9. POJ 题目2411 Mondriaan's Dream(状压DP)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 787 ...

随机推荐

  1. Spring中使用事务搭建转账环境 转账操作,

    演示不使用事务出现异常情况 Dao层两个方法lessMoney()和moreMoney() package com.swift; import org.springframework.jdbc.cor ...

  2. cocos2dx 使用XMLHttpRequest时回调status为0的问题

    今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...

  3. jq封装插件,简单dome

    (function($) { $.fn.extend({ bold: function() { this.css({ fontWeight: "bold", color: 'red ...

  4. python--以1-31的数字作为结尾的列表?论英文好的重要性!

    一.python基础教程第2板(修订版)[代码清单2-1]中有一段要求打印‘以1-31的数字作为结尾的列表’ 截取代码示例:endings =['st','nd','rd'] +17*['th'] + ...

  5. 利用wget 和 curl 监控网站是否正常

    监控网站URL是否正常最常见的方法莫过于wget和curl命令了,这两个命令都是非常强大,参数也非常多,下面列举几个常用的参数. wget  常用命令参数:--spider              ...

  6. LeetCode(189) Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  7. 笔记-python-*号解包

    笔记-python-*号解包 在码代码时发现*号可以这样使用: str = ["abcd", "abce", "abcf"]st = &qu ...

  8. action属性和data属性组合事例

  9. 聊聊、Java 网络编程

    Socket 编程大家都不陌生,Java 学习中必学的部分,也是 Java网络编程核心内容之一.Java 网络编程又包括 TCP.UDP,URL 等模块.TCP 对应 Socket模块,UDP 对应  ...

  10. change login screen wallpaper on ubuntu14.04

    install lightdm-gtk-greeter $ apt-get install lightdm config lightdm $ vim /etc/lightdm/lightdm-gtk- ...