您的位置:首页 > 其它

hdu 4064 Carcassonne

2011-10-07 17:50 525 查看
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




Source
The 36th ACM/ICPC Asia Regional Fuzhou Site ——
Online Contest


Recommend
lcy

今天比赛5题鸭梨比较大,其中蒙了两题规律……orz……

这题是比较明显的状态dp,先预处理每一行有多少种可行的放法,对于某一种放法上面的状态为i,下面的状态为j,这样就有一种方法从状态i转移到状态j。

之后各种MLE……加了滚动数组之后TLE。

后来剪了枝,如果有某一块四面都一样的就不重复算,直接把ans乘以4,这样就可以过了。359MS,这个剪枝好强悍……

代码

#include <stdio.h>
#include <string.h>

#define MOD 1000000007

int map[15][15][5];
char str[5];
int n,m;
long long dp[2][600000];
int three[15];
bool flag;
int tag;

void DFS(int h,int t,int shang,int xia,int right)
{
    int i,j,n,ss,xx,zz,yy;
    if (t==m)
    {
      //  printf("%d..\n",tag);
        if (h==0)
        {
            dp[flag][xia]+=tag;
        }
        else
        {
            dp[flag][xia]=(dp[flag][xia]+dp[flag^1][shang]*tag)%MOD;
        }
        return;
    }
    zz=map[h][t][0];
    ss=map[h][t][1];
    yy=map[h][t][2];
    xx=map[h][t][3];
    if (zz==ss && zz==yy && zz==xx && ss==yy && ss==xx && yy==xx)
    {
        tag*=4;
        if (zz==right || right==-1) DFS(h,t+1,shang*3+ss,xia*3+xx,yy);
        tag/=4;
        return;
    }
    for (i=0;i<4;i++)
    {
        zz=map[h][t][(0+i)%4];
        ss=map[h][t][(1+i)%4];
        yy=map[h][t][(2+i)%4];
        xx=map[h][t][(3+i)%4];
        if (zz==right || right==-1) DFS(h,t+1,shang*3+ss,xia*3+xx,yy);
    }
}

int Change(char s)
{
    if (s=='C') return 0;
    if (s=='R') return 1;
    return 2;
}

int main()
{
    int i,j,T,k,kk,ans,cnt;
    cnt=1;
    three[0]=1;
    for (i=1;i<=13;i++)
    {
        three[i]=three[i-1]*3;
    }
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for (i=0;i<n;i++)
        {
            for (j=0;j<m;j++)
            {
                scanf("%s",str);
                for (k=0;k<4;k++)
                {
                    map[i][j][k]=Change(str[k]);
                }
            }
        }
        memset(dp,0,sizeof(dp));
        flag=0;
        for (i=0;i<n;i++)
        {
            tag=1;
            DFS(i,0,0,0,-1);
            flag=1^flag;
            for (j=0;j<three[m];j++) dp[flag][j]=0;
        }
        ans=0;
        for (i=0;i<three[m];i++)
        {
            ans=(ans+dp[1^flag][i])%MOD;
        }
        printf("Case %d: %d\n",cnt++,ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: