题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4064
Carcassonne
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 326
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.
![](http://acm.hdu.edu.cn/data/images/C348-1004-1.jpg)
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.
![](http://acm.hdu.edu.cn/data/images/C348-1004-2.jpg)
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?
Square tiles are printed by city segments,road segments and field segments.
![](http://acm.hdu.edu.cn/data/images/C348-1004-1.jpg)
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.
![](http://acm.hdu.edu.cn/data/images/C348-1004-2.jpg)
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.
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
Recommend
lcy
题目意思:
每一个格子有四条边,每条边有一种颜色,求通过旋转格子,使相邻格子公共边颜色同样,总的种数。
解题思路:
状态压缩dp
格子不多仅仅有12个,对于每一行维护两种状态,上边的颜色状态up和下边的颜色状态dw,一行搜完后,用上一行的dw状态(也就是当前行的up状态)更新当前行的dw状态。
滚动数组处理。
剪枝:当一个格子四边都是一样颜色的话直接*4,不用再枚举那一条边在上面
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 15 char sa[Maxn][Maxn][5]; ll dp[2][1100000]; int ba[Maxn],n,m,cur,mul; map<char,int>myp; void dfs(int r,int nu,int up,int dw,int ri) { if(nu>m) { if(r==1) { dp[cur][dw]+=mul; dp[cur][dw]%=M; } else { dp[cur][dw]+=(dp[cur^1][up]*mul)%M; dp[cur][dw]%=M; } return ; } int i; for(i=1;i<4;i++) if(sa[r][nu][i]!=sa[r][nu][0]) break; if(i==4) //四个面都同样 { mul*=4; int a=myp[sa[r][nu][0]]; if(ri==-1||a==ri) dfs(r,nu+1,up*3+a,dw*3+a,a); mul/=4; return ; } for(int i=0;i<4;i++) { int uu=myp[sa[r][nu][i]]; int rr=myp[sa[r][nu][(i+1)%4]]; int dd=myp[sa[r][nu][(i+2)%4]]; int L=myp[sa[r][nu][(i+3)%4]]; if(ri==-1||L==ri) dfs(r,nu+1,up*3+uu,dw*3+dd,rr); } } int main() { //cout<<pow(3.0,12.0); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t,cas=0; myp['F']=0; myp['R']=1; myp['C']=2; ba[0]=1; for(int i=1;i<=12;i++) ba[i]=ba[i-1]*3; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%s",sa[i][j]); memset(dp,0,sizeof(dp)); cur=0; for(int i=1;i<=n;i++) { cur=cur^1; mul=1; dfs(i,1,0,0,-1); for(int j=0;j<ba[m];j++) dp[cur^1][j]=0; } ll ans=0; for(int i=0;i<ba[m];i++) { ans+=dp[cur][i]; ans%=M; } printf("Case %d: %d ",++cas,ans); } return 0; }