POJ 2411 Mondriaan's Dream

本文介绍了一种使用状态压缩动态规划(状压DP)的方法来解决二维平面上放置1x2矩形方块的问题。核心思路在于将每一行视为一种状态,通过判断不同状态之间的转移可能性来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嘟嘟嘟

 

这道题感觉和昨天的凉心模拟有点像,不过这一次n和m都是手动输入的,所以矩阵自己画不出来。

但总体的思路还是一样的:状压dp,每一行为一个状态。

考虑每一个状态:对于每一个1 * 2的方块,要么横着放,要么竖着放。竖着放对于这一行相当于这一块的一半。因此,第k位为1表示第 i 行第k列有一个一半的方块第 i + 1行必须为0;0表示这一个块在这一行放完了,对 i + 1行没有影响,因此下一行0或1都行。那么考虑什么状态能从 dp[i - 1][j] 转移到 dp[i][k]:

1.j & k = 0。就是说 i - 1 行是1的位置都要填一个0.

2.j 和 k 按位或,连续的0的长度必须为偶数。就是说i - 1 行有一个00(横着的1 *2方块),i 行必须也有00。

 

然后我们可以把第二个条件预处理。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a) memset(a, 0, sizeof(a))
15 typedef long long ll;
16 typedef double db;
17 const int INF = 0x3f3f3f3f;
18 const db eps = 1e-8;
19 const int maxn = 15;
20 const int max_bin = 1 << 15;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar('-');
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 int n, m;
38 bool odd[max_bin];
39 ll dp[maxn][max_bin];
40 
41 int main()
42 {
43     while((n = read()) && (m = read()))
44     {
45         for(int i = 0; i < (1 << m); ++i)
46         {
47             bool cnt = 0, flg = 0;
48             for(int j = 0; j < m; ++j)
49             {
50                 if(!((1 << j) & i)) cnt ^= 1;
51                 else flg |= cnt, cnt = 0;
52             }
53             odd[i] = flg | cnt ? 0 : 1;
54         }
55         dp[0][0] = 1;
56         for(int i = 1; i <= n; ++i)
57             for(int j = 0; j < (1 << m); ++j)
58             {
59                 dp[i][j] = 0;
60                 for(int k = 0; k < (1 << m); ++k)
61                     if(!(j & k) && odd[j | k]) dp[i][j] += dp[i - 1][k];
62             }
63         write(dp[n][0]); enter;
64     }
65     return 0;
66 }
View Code

 

转载于:https://www.cnblogs.com/mrclr/p/9620714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值