ZOJ Problem Set - 2563 Long Dominoes 【状压dp】

题目:ZOJ Problem Set - 2563 Long Dominoes

题意:给出1*3的小矩形,求覆盖m*n的矩阵的最多的不同的方法数?

分析:有一道题目是1 * 2的,比较火,链接:这里

这个差不多,就是当前行的状态对上一行有影响,对上上一行也有影响。所以

定义状态:dp【i】【now】【up】表示在第 i 行状态为now ,上一行状态为 up 时的方案数。

然后转移方程:dp【i】【now】【up】 = sum ( dp【i-1】【up】【uup】 ) 前提是合法

合法性的判断是比较难考虑的一点,因为是1 * 3的,只能横着放和竖着放。

如果横着放,那么上面up 和 uup 的 3 格也必须是满的才行

如果竖着放,那么up 和 uup的当前格必须是空的,注意判断后变化up

如果用简单的枚举dp的话,四层循环会超时,所以我们要用up 和 upp 来深搜构造当前行,这样才能过。

AC代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define INT long long int
const long long N = 9 ;
const int inf = 0x3f3f3f3f;
INT dp[31][1<<N][1<<N];
int m,n;
bool Judge(int s,int up,int upp)
{
    for(int i=s;i<s+3;i++)
        if(!(up&(1<<i)) || !(upp&(1<<i)))
            return false;
    return true;
}
void dfs(int st,int cnt,int now,int up,int upp,INT num)
{
    if(cnt==m)
    {
        dp[st][now][up]+=num;
        return ;
    }
    if(cnt+3<=m && Judge(cnt,up,upp)) //heng
        dfs(st,cnt+3,now|(1<<cnt)|(1<<(cnt+1))|(1<<(cnt+2)),up,upp,num);
    if(!(up&(1<<cnt))&&!(upp&(1<<cnt))) // 竖放
        dfs(st ,cnt+1 ,now|(1<<cnt) ,up|(1<<cnt) ,upp , num) ;
    if(upp&(1<<cnt))  //留空
       dfs(st ,cnt+1 ,now ,up ,upp ,num) ;
}
int main()
{
    while(~scanf("%d%d",&m,&n)&& m+n)
    {
        memset(dp,0,sizeof(dp));
        dfs(1,0,0,(1<<m)-1,(1<<m)-1,1);
        for(int i=2; i<=n; i++)
        {
            for(int up=0; up<(1<<m); up++) //上行
            {
                for(int st=0; st<(1<<m); st++) //上上行
                {
                    if(dp[i-1][up][st])  //注意一定判断
                        dfs(i,0,0,up,st,dp[i-1][up][st]);
                }
            }
        }
        printf("%lld\n",dp[n][(1<<m)-1][(1<<m)-1]);
    }
    return 0;
}

枚举超时代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define INT long long int
const long long N = 9 ;
const int inf = 0x3f3f3f3f;
INT dp[31][1<<N][1<<N];
int m,n;
bool isit(int st)
{
    int tmp=0;
    for(int i=0;i<m;i++)
    {
        if(st&(1<<i))
            tmp++;
        else
        {
            if(tmp%3)
                return false;
        }
    }
    if(tmp%3)
        return false;
    return true;
}
INT solve(int now,int up,int uup)
{
    for(int i=0;i<m;i++)
    {
        if(!(uup&(1<<i))) //判断上上一行为0
        {
            if(up&(1<<i))
                return -1 ;
            else
            {
                if(!(now&(1<<i)))
                     return -1 ;
                else
                    up |= (1<<i) ;
            }
        }
        else
        {
            if(up&(1<<i) && now&(1<<i)) // 1 1 1
            {
                if(i==m-2 || i==m-1)
                    return -1 ;
                else if(!(now&(1<<(i+1))) || !(now&(1<<(i+2))) || !(up&(1<<(i+1))) || !(up&(1<<(i+2))) || !(uup&(1<<(i+2))) || !(uup&(1<<(i+1))))
                    return -1 ;
                i+=2;
            }
        }
    }
    return up ;
}
int main()
{
    while(~scanf("%d%d",&m,&n)&& m+n)
    {
        for(int st=0;st<(1<<m);st++)
        {
            if(!isit(st))
                continue;
            for(int up=0;up<(1<<m);up++)
            {
                if(isit(up)){
                    dp[2][st][up]=1;
                }
            }
        }
        for(int i=3;i<=n;i++)
        {
            for(int no = 0;no < (1<<m); no++)
            {
                for(int kp=0;kp<(1<<m);kp++)  //上行
                    dp[i][no][kp]=0;
                for(int up=0;up<(1<<m);up++)  //上行
                {
                    int temp ;
                    for(int st=0;st<(1<<m);st++) //上上行
                        if((temp = solve(no,up,st)) != -1)
                            dp[i][no][temp]+=dp[i-1][up][st];
                    //printf("%d\n",dp[i][up][up]);
                }
            }
        }
        printf("%lld\n",dp[n][(1<<m)-1][(1<<m)-1]);
    }
    return 0;
}
时间: 2024-10-13 16:12:23

ZOJ Problem Set - 2563 Long Dominoes 【状压dp】的相关文章

ZOJ Problem Set - 2297 Survival 【状压dp】

题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和可以增加的血,然后有一个boss,必须把小怪所有都打死之后才能打boss,血量小于0会死,也不能大于100. 分析:定义状态:dp[st],表示在 st 状态下的血量. 然后转移:dp[st] = max (dp[st],dp[st&~(1<<i )]+p[i].first - p[i].second); 注意初始化的时候必须在开始初始化,否则容易出错. #include &

zoj zju 2994 Tiling a Grid With Dominoes 状压dp

Tiling a Grid With Dominoes Time Limit: 2 Seconds      Memory Limit: 65536 KB We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five differen

HDU 1992 Tiling a Grid With Dominoes (状压 dp)

Problem Description We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five different ways that a grid 4 units high and 2 units wide may be ti

2018 Multi-University Training Contest 3 1003 / hdu6321 Problem C. Dynamic Graph Matching 状压dp

Problem C. Dynamic Graph Matching 题意: 给定一个n个点的无向图,m次加边或者删边操作.在每次操作后统计有多少个匹配包含k= 1,2,...,n2条边. 2≤n≤10,1≤m≤30000. Shortest judge solution: 770 bytes 题解: 设f[i][S]表示前i次操作之后,S集合的点已经匹配的方案数. 对于加边操作,显然f[i][S] =f[i?1][S] +f[i?1][S?u?v].i这一维可以省略,从大到小遍历S,f[S]+

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

ZOJ 3306 状压dp

转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds Memory Limit: 32768 KB In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

ZOJ 3471 Most Powerful 状压DP

水题,一维的DP,表示还剩哪些atom的时候能获得的最大能量 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <

ZOJ 3471 Most Powerful(状压DP)

Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two at