zoukankan      html  css  js  c++  java
  • 状态压缩动态规划 -- 骨牌


    使用1*2 的骨牌通过组合拼成 m * n 的大矩形。问有几种拼法

    题目链接:http://poj.org/problem?

    id=2411

    状态转移:

    1.因为上一行的该列竖直放置骨牌为 0。影响到当前行的该列,当前行的该列为 1

    2.当前行骨牌横放。上一行骨牌横放。 都为11

    3.上一行该列置为 1。当前行当前列立着放为 0


    #include <iostream>
    #include <cstring>
    using namespace std;
    #define MAXSIZE 12
    int cols, raws;
    int col, raw;
    long long DP[MAXSIZE][( 1 << MAXSIZE ) - 1];
    
    void search( int col, int cur, int pre ){
        if( col >= cols ){
            if( col == cols ){
                DP[raw][cur] += DP[raw - 1][pre];
            }
            return;
        }
        search( col + 1, cur << 1, pre << 1 | 1 );
        search( col + 1, cur << 1 | 1, pre << 1 );
        search( col + 2, cur << 2 | 3, pre << 2 | 3 );
    }
    
    int main(){
        while( cin >> raws >> cols ){
            if( raws == 0 && cols == 0 )
                break;
            if( cols > raws ){
                raws = raws ^ cols;
                cols = raws ^ cols;
                raws = raws ^ cols;
            }
            memset( DP, 0, sizeof( DP ) );
            DP[0][( 1 << cols ) - 1] = 1;
    
            for( raw = 1; raw <= raws; ++raw )
                search( 0, 0, 0 );
    
            cout << DP[raws][( 1 << cols ) - 1] << endl;
        }
        return 0;
    }
    


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    CDN的简单理解
    学习前端笔记1(HTML)
    HTTP概念解析
    web前端
    Bootstrap_排版_标题
    Bootstrap_表单_表单样式
    Bootstrap_排版_列表
    Bootstrap_表单_按钮
    Bootstrap_表单_表单控件
    Bootstrap_基本HTML模板
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4666501.html
Copyright © 2011-2022 走看看