Carcassonne - HDU 4064 状压dp

来源:互联网 发布:天津平山道淘宝城 编辑:程序博客网 时间:2024/04/28 23:27

Carcassonne

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 897    Accepted Submission(s): 341


Problem Description
Carcassonne is a tile-based board game for two to five players.
Square tiles are printed by city segments,road segments and field segments. 

The rule of the game is to put the tiles alternately. Two tiles share one edge should exactly connect to each other, that is, city segments should be linked to city segments, road to road, and field to field. 

To simplify the problem, we only consider putting tiles:
Given n*m tiles. You can rotate each tile, but not flip top to bottom, and not change their order. 
How many ways could you rotate them to make them follow the rules mentioned above?
 

Input
The first line is a number T(1<=T<=50), represents the number of case. The next T blocks follow each indicates a case.
Each case starts with two number N,M(0<N,M<=12)
Then N*M lines follow,each line contains M four-character clockwise.
'C' indicate City.
'R' indicate Road.
'F' indicate Field.
 

Output
For each case, output the number of ways mod 1,000,000,007.(as shown in the sample output)
 

Sample Input
31 1RRRR1 2RRRF FCCC8 8FCFF RRFC FRCR FRFR RCCR FFCC RRFF CRFRFRRC FRFR CCCR FCFC CRRC CRRR FRCR FRFRRRCR FRRR CCCR FFFC RRFF RFCR CCFF FCCCCFCF RRFF CRFR FFRR FRRF CCRR FFFC CRRFCFRR FFFF FFFF RRFF RRRR RCRR FFCC RFRFRRCF FRFR FRRR FRFR RCCR RCCC CFFC RFRFCFCF FRFF RRFF FFFF CFFF CFFF FRFF RFRRCCRR FCFC FCCC FCCC FFCC FCCF FFCC RFRF
 

Sample Output
Case 1: 4Case 2: 1Case 3: 1048576
 


题意:拼图的可以旋转,要求相邻的两块拼图相连的字母相同,问一共有多少种方式。

思路:对于每一行进行搜索,找到所有合适的上边界状压结果和下边界状压结果,然后用滚动数组进行转移。有一个小优化,当一个拼图的四个边相同时,只搜索一次,结果*4即可。

AC代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;ll dp[2][600010],MOD=1e9+7;int T,t,n,m;char s[13][13][6];int num[15],POW[20],a,b,val[200],S,S2,TIP;bool same[15];void pai(int x,int ret){    int i;    if(ret>m)    {        if(x==1)          dp[b][S2]=(dp[b][S2]+TIP)%MOD;        else          dp[b][S2]=(dp[b][S2]+dp[a][S]*TIP%MOD)%MOD;    }    else    {        if(same[ret])        {            if(ret==1 || s[x][ret-1][(num[ret-1]+1)%4]==s[x][ret][0] )            {                num[ret]=0;                S+=val[s[x][ret][0]]*POW[ret];                S2+=val[s[x][ret][0]]*POW[ret];                pai(x,ret+1);                S-=val[s[x][ret][0]]*POW[ret];                S2-=val[s[x][ret][0]]*POW[ret];            }        }        else        for(i=0;i<4;i++)           if(ret==1 || s[x][ret-1][(num[ret-1]+1)%4]==s[x][ret][(i+3)%4] )           {               num[ret]=i;               S+=val[s[x][ret][i]]*POW[ret];               S2+=val[s[x][ret][(i+2)%4]]*POW[ret];               pai(x,ret+1);               S-=val[s[x][ret][i]]*POW[ret];               S2-=val[s[x][ret][(i+2)%4]]*POW[ret];           }    }}int main(){    int i,j,k;    ll ans;    POW[1]=1;    for(i=2;i<=15;i++)       POW[i]=POW[i-1]*3;    val['C']=0;val['F']=1;val['R']=2;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)           for(j=1;j<=m;j++)              scanf("%s",s[i][j]);        memset(dp,0,sizeof(dp));        for(i=1;i<=n;i++)        {            if(i&1)              a=0,b=1;            else              a=1,b=0;            memset(dp[b],0,sizeof(dp[b]));            memset(same,0,sizeof(same));            TIP=1;            for(j=1;j<=m;j++)               if(s[i][j][0]==s[i][j][1] && s[i][j][1]==s[i][j][2] && s[i][j][2]==s[i][j][3])               {                   same[j]=1;                   TIP*=4;               }            pai(i,1);        }        ans=0;        for(i=0;i<=POW[13];i++)           ans+=dp[b][i];        ans%=MOD;        printf("Case %d: %I64d\n",t,ans);    }}



0 0
原创粉丝点击