HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064

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. 
HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
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. 
HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
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)
 
题目大意:给一个n*m的矩阵,每个格子有一个方块,给出方块顺时针方向的每条边颜色,方块可以旋转,要求相邻的边颜色相同,问有多少种方案。
思路:以颜色做状态,3种颜色,用4进制。就是一个普通的插头DP,好长时间没写插头DP了结果调了半天T_T。
 
代码(15MS):
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ;
const int SIZE = ;
const int MOD = 1e9 + ; struct Hashmap {
int head[SIZE], ecnt;
int to[SIZE], next[SIZE], val[SIZE];
int stk[SIZE], top; Hashmap() {
memset(head, -, sizeof(head));
} void clear() {
while(top) head[stk[--top]] = -;
ecnt = ;
//for(int i = 0; i < SIZE; ++i) if(head[i] != -1) cout<<"error"<<endl;
} void insert(int st, int value) {
int h = st % SIZE;
for(int p = head[h]; ~p; p = next[p]) {
if(to[p] == st) {
val[p] += value;
if(val[p] >= MOD) val[p] -= MOD;
return ;
}
}
if(head[h] == -) stk[top++] = h;
to[ecnt] = st; val[ecnt] = value; next[ecnt] = head[h]; head[h] = ecnt++;
}
} hashmap[], *pre, *cur; char s[MAXN][MAXN][];
int w[];
int n, m, T; int getState(int state, int i) {
return (state >> (i << )) & ;
} void setState(int &state, int i, int val) {
i <<= ;
state = (state & ~( << i)) | (val << i);
} int solve() {
pre = &hashmap[], cur = &hashmap[];
cur->clear();
cur->insert(, );
int maxState = ( << ((m + ) << )) - ;
for(int i = ; i < n; ++i) {
for(int p = ; p < cur->ecnt; ++p)
cur->to[p] = (cur->to[p] << ) & maxState;
for(int j = ; j < m; ++j) {
swap(pre, cur);
cur->clear();
for(int p = ; p < pre->ecnt; ++p) {
int st = pre->to[p];
for(int k = ; k < ; ++k) {
if(j != && w[(int)s[i][j][(k + ) & ]] != getState(st, j)) continue;
if(i != && w[(int)s[i][j][(k + ) & ]] != getState(st, j + )) continue;
int new_st = st;
setState(new_st, j, w[(int)s[i][j][k]]);
setState(new_st, j + , w[(int)s[i][j][(k + ) & ]]);
cur->insert(new_st, pre->val[p]);
}
}
}
}
int res = ;
for(int p = ; p < cur->ecnt; ++p) {
res += cur->val[p];
if(res >= MOD) res -= MOD;
}
return res;
} int main() {
w['F'] = ; w['C'] = ; w['R'] = ;
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) scanf("%s", s[i][j]);
printf("Case %d: %d\n", t, solve());
}
}
上一篇:微信群100人限制解除 群主可继续邀请朋友进群


下一篇:[代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传