poj2411(状态转移,dfs搜索转移)

题目链接

题意:用1*2的小矩形拼成n*m(n,m<=11)的矩阵有多少种方案。

解法:状态压缩,dfs求转移。当前一列的状态确定后,后一列必须用横向的矩形来填补前一列的空白格,所以前一列不为空时,这一列可以选择也为空让下一列来填补,或是本列来个纵向的矩形填补(如果有连续的两个空格的话)。

代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=100010;
const LL INF=0x3FFFFFFF;
LL dp[2][(1<<12)];
int n,m;
void dfs(int now,int wei,int st,LL num)
{
    if(wei==m)
    {
        dp[now][st]+=num;
        return ;
    }
    dfs(now,wei+1,st,num);
    if(wei<m-1&&!(st&(1<<wei))&&!(st&(1<<(wei+1))))
        dfs(now,wei+2,st|(1<<wei)|(1<<(wei+1)),num);
}
int main()
{
    while(cin>>n>>m&&n+m)
    {
        int now=0;
        memset(dp,0,sizeof dp);
        dfs(now,0,0,1);
        for(int i=1; i<n; i++)
        {
            memset(dp[now^1],0,sizeof dp[now]);
            for(int j=0; j<(1<<m); j++)
                if(dp[now][j])
                    dfs(now^1,0,(~j)&((1<<m)-1),dp[now][j]);
            now^=1;
        }
        printf("%I64d\n",dp[now][(1<<m)-1]);
    }
    return 0;
}
/*
n*1+((n-1)/2+(n-2)/3)*/
时间: 2024-08-27 05:42:26

poj2411(状态转移,dfs搜索转移)的相关文章

POJ2411——状态压缩+DFS——Mondriaan&#39;s Dream

Description 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 r

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

POJ2411 状态压缩

Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11208 Accepted: 6521 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (w

[ZOJ 1011] NTA (dfs搜索)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. 给了树的前序遍历序. 直接dfs搜索. 1 #include <cstdio> 2 #include <cstdlib> 3 #include <string> 4 #include <iostream> 5 #include <cstring>

hdu1372 dfs搜索之国际象棋的马

原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到另一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四个方向.而是由"日" 字组成的8个方向.虽然是国际象棋的马,但是其实和中国象棋的马走法还是一样的. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std;

URAL 1298 knight dfs搜索

1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard. No, he did not like usual chess game, and in addition, he d

nyist oj 19 擅长排列的小明(dfs搜索+STL)

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int