# Carcassonne

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

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.
'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

Recommend
lcy

#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)
}
}
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;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客