Carcassonne - HDU 4064 状压dp

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
  
  
3 1 1 RRRR 1 2 RRRF FCCC 8 8 FCFF RRFC FRCR FRFR RCCR FFCC RRFF CRFR FRRC FRFR CCCR FCFC CRRC CRRR FRCR FRFR RRCR FRRR CCCR FFFC RRFF RFCR CCFF FCCC CFCF RRFF CRFR FFRR FRRF CCRR FFFC CRRF CFRR FFFF FFFF RRFF RRRR RCRR FFCC RFRF RRCF FRFR FRRR FRFR RCCR RCCC CFFC RFRF CFCF FRFF RRFF FFFF CFFF CFFF FRFF RFRR CCRR FCFC FCCC FCCC FFCC FCCF FFCC RFRF
 

Sample Output
  
  
Case 1: 4 Case 2: 1 Case 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值