您的位置:首页 > 其它

hdu 4064 (插头DP)

2011-10-07 18:09 489 查看

Carcassonne

[b]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 75 Accepted Submission(s): 8

[/b]

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


题目:http://acm.hdu.edu.cn/showproblem.php?pid=4064
分析:这题只要看懂题之后,而且做过插头DP的话,其实算是1道水题了,这题可以假想为三类插头,就是三种类型的建筑,然后用四进制的插头DP来搞,由于这题转移时没有什么限制,只好在每次换到下一行时写一个取位,因为这个问题找了好久才过了样例,然后怕内存爆了开大一点结果TLE,小一点就A了= =
代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=100007;
const int mod=1000000007;
struct hashmap
{
    int h[mm],s[mm],p[mm],d[mm],t;
    int hash(int x,int dd)
    {
        int c=x%mm,i;
        for(i=h[c];i>=0;i=p[i])
        if(s[i]==x)
        {
            d[i]+=dd;
            if(d[i]>=mod)d[i]%=mod;
            return i;
        }
        s[t]=x,d[t]=dd,p[t]=h[c],h[c]=t;
        return t++;
    }
    void clear()
    {
        t=0;
        memset(h,-1,sizeof(h));
    }
}f[2];
int i,j,k,g1,g2,g,n,m,t,ans,cas=0,save;
char v[111],w[256];
bool ok(int a,int b)
{
    return (a==0||a==b);
}
void link(int s,int d)
{
    int e,x=(s>>(j<<1))&3,y=(s>>((j+1)<<1))&3;
    s=s^(x<<(j<<1))^(y<<((j+1)<<1));
    for(e=0;e<4;++e)
        if(ok(x,v[e])&&ok(y,v[(1+e)%4]))
            f[g2].hash(s|(v[(3+e)%4]<<(j<<1))|(v[(2+e)%4]<<((j+1)<<1)),d);
}
int main()
{
    scanf("%d",&t);
    w['C']=1,w['F']=2,w['R']=3;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        save=(1<<(m<<1))-1;
        f[0].clear();
        f[0].hash(0,1);
        for(g1=1,g2=i=0;i<n;++i)
        {
            for(j=0;j<f[g2].t;++j)f[g2].s[j]=(f[g2].s[j]&save)<<2;
            for(j=0;j<m;++j)
            {
                scanf("%s",v);
                for(k=0;k<4;++k)v[k]=w[v[k]];
                g1=!g1,g2=!g2,f[g2].clear();
                for(k=0;k<f[g1].t;++k)
                    link(f[g1].s[k],f[g1].d[k]);
            }
        }
        for(ans=i=0;i<f[g2].t;++i)
        {
            ans+=f[g2].d[i];
            if(ans>=mod)ans%=mod;
        }
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: