您的位置:首页 > 其它

hdu 4064 Carcassonne(DP-插头DP)

2014-09-03 21:07 393 查看


Carcassonne

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

Total Submission(s): 807 Accepted Submission(s): 313



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
本人不会3进制,所以用string来保存状态= =


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

const int maxn = 13;
const int mod = 1000000007;
struct Node{
char left , right , down , up;
Node(char a = 'A' , char b = 'A' , char c = 'A' , char d = 'A'){
up = a , down = b , left = c , right = d;
}
}node[maxn][maxn][4];
map<string , int> dp[maxn][maxn];
int N , M;

void initial(){
for(int i = 0; i < maxn; i++){
for(int j = 0; j < maxn; j++) dp[i][j].clear();
}
}

void Rotate(){
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
for(int k = 1; k < 4; k++){
node[i][j][k].up = node[i][j][k-1].left;
node[i][j][k].right = node[i][j][k-1].up;
node[i][j][k].down = node[i][j][k-1].right;
node[i][j][k].left = node[i][j][k-1].down;
}
}
}
}

void readcase(){
scanf("%d%d" , &N , &M);
char block[5];
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
scanf("%s" , block);
node[i][j][0].up = block[0];
node[i][j][0].right = block[1];
node[i][j][0].down = block[2];
node[i][j][0].left = block[3];
}
}
Rotate();
}

int DP(int i , int j , string sta){
if(i >= N) return 1;
if(dp[i][j].find(sta) != dp[i][j].end()) return dp[i][j][sta];
int ans = 0;
for(int k = 0; k < 4; k++){
if((node[i][j][k].up == sta[j] || i == 0) && (node[i][j][k].left == sta[M] || j == 0)){
string tsta = sta;
tsta[M] = node[i][j][k].right;
tsta[j] = node[i][j][k].down;
if(j+1<M) ans = (ans+DP(i , j+1 , tsta))%mod;
else ans = (ans+DP(i+1 , 0 , tsta))%mod;
}
}
return dp[i][j][sta] = ans%mod;
}

void computing(){
string sta;
for(int i = 0; i <= M; i++) sta.push_back('A');
printf("%d\n" , DP(0 , 0 , sta)%mod);
}

int main(){
int T;
scanf("%d" , &T);
for(int i = 1; i <= T; i++){
initial();
readcase();
printf("Case %d: " , i);
computing();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: