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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
ZOJ Problem Set - 2563 Long Dominoes 【如压力dp】的更多相关文章
- ZOJ Problem Set - 2297 Survival 【状压dp】
题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1025解题报告
ZOJ Problem Set - 1025 题目分类:基础题 原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=10 ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- ZOJ Problem Set - 3593 拓展欧几里得 数学
ZOJ Problem Set - 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 One Person ...
- ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】
题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...
- ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】
题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- ZOJ Problem Set - 3819Average Score
ZOJ Problem Set - 3819Average Score 题目链接 题目大意:给你两个班的的学生的分数(A,B班).A班有一个学生的分数没有给出. 如今要求你给出这个学生分数的上下限.使 ...
随机推荐
- window.open()具体解释及浏览器兼容性问题
一.基本的语法:window.open(pageURL,name,parameters)当中:pageURL 为子窗体路径name 为子窗体名字parameters 为窗体參数(各參数用逗号分隔) ...
- Codeforces Round #246 (Div. 2) —B. Football Kit
B. Football Kit time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- cocos2d-x2.2.5 + cocos2d-x3.2鸟跳便宜源代码“开源”
尊重开发人员的劳动成果,转载请注明From郝萌主 游戏简单介绍: 贱鸟跳跳,贱贱的小鸟这次遇上大问题了.被它整蛊过的同类都在找它的麻烦,如今我们赶紧到游戏中帮帮它吧!左右手互撸,合理操控.获得高分,打 ...
- hdu5119(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5119 分析:dp[i][j]表示由前i个数组成异或和为j的方法数,则dp[i][j]=d[i-1][j ...
- Jndi使用好处,与简单实例【JBOSS】
JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...
- codeforces 112APetya and Strings(字符串水题)
A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 安装pygame
pygame的安装 我们首先要去到:http://www.pygame.org/download.shtml 下载我们所需要的软件包: 我选择的是:pygame-1.9.2a0.win32-py3.2 ...
- VBA怎样统计同一类型的数据的总和
今天是2014-11-01 是周末,忙了一周了,最终能够闲下来了.想起近期工作用到的VBA的一个场景,结合VBA的数组,所以就想试试看.结果还好.出来了.这年头,又玩起了VB了,经过多时才接受了VB的 ...
- Error: ORA-16501: the Data Guard broker operation failed ORA-16625: cannot reach database
在备库上建配置文件 DGMGRL> create configuration 'sharkdbbork' as primary database is 'sharkdb' connect ide ...
- lambda left join .DefaultIfEmpty
我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘 ...