BZOJ 2331 [SCOI2011]地板

題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id=2331


題意:給一個n*m的格子,有一些地方可以放置,有一些地方不可以放置,現在只用'L'型的地板鋪滿整個格子,L型地板的兩端長度可以任意變化,但不能長度爲0。鋪設完成後,可以放置的地方都必須鋪上地板,但同一個地方不能被鋪多次,求不同的鋪法。


思路:插頭dp從上到下,左到右逐格遞推,所以我們只定義向下和向右兩個方向,那麼對於任意的L型的格子內部的連通線,在向下和向右的方向上只能是進入到拐點或從拐點內出。我們就用4進製表示輪廓線,0表示此處無連通,1表示(向下/向右方向上)一個入的連通線,2表示(向下/向右方向上)一個出的連通線。因爲L型兩端的長度至少爲1,所以只要保證有一個連通線的長度就可以了,一個連通線跨越兩格,除去拐點格,還剩下一格長度。然後就是根據上方和左方的連通性來轉移狀態。n*m<=100,所以若m>n就翻轉地圖,讓小的成爲m。


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define Clean(x,y) memset(x,y,sizeof(x))

int n,m;
int pre,cur;

char g[100][100];
const int Hash = 10007;
const int maxn = 2009999;
const int mod = 20110520;
LL ans;
int bit = 3;
int inc = 2;
int code[30];

struct hash_table
{
    int head[Hash] , next[maxn];
    LL state[maxn],value[maxn];
    int size;
    void clear()
    {
        size = 0;
        Clean(head,-1);
    }
    void push( LL S , LL V )
    {
        int index = S % Hash;
        for( int k = head[index]; k != -1; k = next[k] )
            if ( state[k] == S )
            {
                value[k] = (value[k] + V) % mod;
                return;
            }
        state[size] = S , value[size] = V;
        next[size] = head[index] , head[index] = size++;
    }
}dp[2];

inline void decode( LL S , int m )
{
    for( int i = 0; i <= m; i++ ) code[i] = S & bit , S >>= inc;
}

inline LL encode( int m )
{
    LL ans = 0;
    for( int i = m; i >= 0; i-- )
    {
        ans <<= inc;
        ans |= code[i];
    }
    return ans;
}

bool check( int m )
{
    for( int i = 0; i <=m; i++ ) if ( code[i] ) return false;
    return true;
}

void DP( int x , int y , int k )
{
    decode( dp[pre].state[k] , m );
    int left = code[y-1] , up = code[y];
    LL V = dp[pre].value[k];
    code[y] = code[y-1] = 0;

    if ( g[x][y] == '*' ) //當前格子不可放置
    {
        if ( !left && !up ) dp[cur].push( encode(m) , V ); //左 上 必須無連通纔可以轉移
    }
    else
    {
        if ( !left && !up ) //左、上 無連通
        {
            //當前格子向下延伸成爲下方某個拐點的一條邊
            if ( x < n && g[x+1][y] == '_' ) code[y-1] = 1 , dp[cur].push( encode(m) , V ) , code[y-1] = 0;
            //當前格子向右延伸成爲右方某個拐點的一條邊
            if ( y < m && g[x][y+1] == '_' ) code[y] = 1 , dp[cur].push( encode(m) , V ) , code[y] = 0;
            //當前格子可以作爲一個拐點格,向下,右延伸。
            if ( x < n && y < m && g[x+1][y] == '_' && g[x][y+1] == '_' ) code[y] = 2 , code[y-1] = 2 , dp[cur].push( encode(m) , V );
        }
        else if ( !left || !up ) //有一個連通
        {
            if ( up == 1 )//上方作爲入連通 , 也就是說下方應該存在一個拐點
            {
                //繼續延伸
                if ( x < n && g[x+1][y] == '_' ) code[y-1] = 1 , dp[cur].push( encode(m) , V ) , code[y-1] = 0;
                //停止延伸 , 此格子作爲拐點 , 並且該向右拐出去
                if ( y < m && g[x][y+1] == '_' ) code[y] = 2 , dp[cur].push( encode(m) , V ) , code[y] = 0;
            }
            else if ( up == 2 ) //作爲上方某個拐點的出邊
            {
                //繼續出
                if ( x < n && g[x+1][y] == '_' ) code[y-1] = 2 , dp[cur].push( encode(m) , V ) , code[y-1] = 0;
                //停止
                dp[cur].push( encode(m) , V );
            }
            else if ( left == 1 ) //同上
            {
                if ( x < n && g[x+1][y] == '_' ) code[y-1] = 2 , dp[cur].push( encode(m) , V ) , code[y-1] = 0;
                if ( y < m && g[x][y+1] == '_' ) code[y] = 1 , dp[cur].push( encode(m) , V ) , code[y] = 0;
            }
            else
            {
                dp[cur].push( encode(m) , V );
                if ( y < m && g[x][y+1] == '_' ) code[y] = 2 , dp[cur].push( encode(m) , V ) , code[y] = 0;
            }
        }
        else //雙聯通
        {
            if ( left == up && left == 1 ) //必須相等 且都爲1,那麼此格子可以作爲一個拐點
            {
                dp[cur].push( encode(m) , V );
            }
        }
    }
}

LL solve()
{
    cur = 0;
    dp[0].clear();
    dp[0].push( 0 , 1 );

    for( int i = 1; i <= n; i++ )
    {
        pre = cur , cur ^= 1;
        dp[cur].clear();
        for( int k = 0; k < ( dp[pre].size ); k++ )
        dp[cur].push( dp[pre].state[k]<<inc , dp[pre].value[k] );

        for( int j = 1; j <= m; j++ )
        {
            pre = cur , cur ^= 1 , dp[cur].clear();
            for( int k = 0; k < dp[pre].size; k++ ) DP( i , j , k );
        }
    }
    for( int k = 0; k < dp[cur].size; k++ )
        if ( dp[cur].state[k] == 0 ) return dp[cur].value[k];
    return 0;
}

void init()
{
    char s[100];
    getchar();
    for (int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		for (int j=1;j<=m;j++)
			if (n<m) g[j][i] = s[j];
		    else g[i][j] = s[j];
	}
	if (n<m) swap(n,m);
}

int main()
{
        scanf("%d%d",&n,&m);
        init();
        printf("%lld\n",solve());
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章