题目链接: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. 

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)
 
题目大意:给一个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());
}
}

最新文章

  1. json的注意事项
  2. CSS3利用text-shadow属性实现多种效果的文字样式展现
  3. 每一个C#开发者必须知道的13件事情
  4. iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
  5. swift学习初步(四)-- 函数
  6. HDOJ(HDU) 2162 Add ‘em(求和)
  7. Foundation Data Structure
  8. sass玩转颜色总结笔记
  9. 【转】关于python中re模块split方法的使用
  10. linq 在查询表达式中处理异常
  11. Remove Google Play Games libraries on iOS (Unity3D开发之二十一)
  12. Confluence 6 使用 Velocity 宏
  13. SpringBoot文件上传下载
  14. C语言第十讲,枚举类型简单说明
  15. SpringBoot简单的REST风格例子
  16. django cookie与session组件
  17. mybatis四大接口之 Executor
  18. JSP指示元素&lt;%@ %&gt; 与指示类型
  19. JavaBean示例
  20. 编程之美 set 11 买书问题

热门文章

  1. ServletDemo
  2. Windows下mysql自动备份的最佳方案
  3. Scrum 的相关概念
  4. HBase HDFS目录树
  5. 【转】Swing 与EDT线程
  6. BI 商业智能理解结构图
  7. ajax处理回调函数,用ajax向后台发送数据
  8. JS-005-常见下拉列表 Select 和 datalist
  9. [代码片段]读取BMP文件(二)
  10. iOS苹果开发者客服电话地址