题目:

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. UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息

    使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...

  2. ReactiveCocoa入门-part2

    ReactiveCocoa是一个框架,它能让你在iOS应用中使用函数响应式编程(FRP)技术.在本系列教程的第一部分中,你学到了如何将标准的动作与事件处理逻辑替换为发送事件流的信号.你还学到了如何转换 ...

  3. 一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师,请教大神

    一个4年工作经验的java程序员的困惑,怎样才能能为一个架构师 LZ本人想往架构师发展, 业余时间也会看一些书籍, 但是感觉没有头绪, 有些书看了,也没有地方实践 我做了4年的java开发, 在一个公 ...

  4. python中打印金字塔和九九乘法表的几种方法

    # 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...

  5. 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装

    参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...

  6. MIP启发式算法:Variable Neighborhood Decomposition Search

    *本文记录和分享学习到的知识,算不上原创. *参考文献见链接. 本文主要简述和VND VNS RINS很相关的vairable neighborhood decomposition search. 目 ...

  7. WPF触控程序开发(二)——整理的一些问题

    上一篇(WPF触控程序开发)介绍了几个比较不错的资源,比较基础.等到自己真正使用它们时,问题就来了,现把我遇到的几个问题罗列下,大家如有遇到其他问题或者有什么好的方法还望赐教. 问题1.如何获取触控点 ...

  8. 数据挖掘算法——Apriori

    在上一篇数据挖掘入门算法整理中提到,Apriori算法是关联规则算法中使用最为广泛的算法,这次我们就来学习下该算法的基本知识. 一.算法概述 Apriori 算法是一种最有影响力的挖掘布尔关联规则的频 ...

  9. Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)

    题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1.  如果两个链表都空,则返回null; 2.  如果链表1空,则返回链表2的 ...

  10. python - 目录处理

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_文件目录操作.py@ide: PyCharm Community ...