题目链接: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):
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. typedef long long LL;
  7.  
  8. const int MAXN = ;
  9. const int SIZE = ;
  10. const int MOD = 1e9 + ;
  11.  
  12. struct Hashmap {
  13. int head[SIZE], ecnt;
  14. int to[SIZE], next[SIZE], val[SIZE];
  15. int stk[SIZE], top;
  16.  
  17. Hashmap() {
  18. memset(head, -, sizeof(head));
  19. }
  20.  
  21. void clear() {
  22. while(top) head[stk[--top]] = -;
  23. ecnt = ;
  24. //for(int i = 0; i < SIZE; ++i) if(head[i] != -1) cout<<"error"<<endl;
  25. }
  26.  
  27. void insert(int st, int value) {
  28. int h = st % SIZE;
  29. for(int p = head[h]; ~p; p = next[p]) {
  30. if(to[p] == st) {
  31. val[p] += value;
  32. if(val[p] >= MOD) val[p] -= MOD;
  33. return ;
  34. }
  35. }
  36. if(head[h] == -) stk[top++] = h;
  37. to[ecnt] = st; val[ecnt] = value; next[ecnt] = head[h]; head[h] = ecnt++;
  38. }
  39. } hashmap[], *pre, *cur;
  40.  
  41. char s[MAXN][MAXN][];
  42. int w[];
  43. int n, m, T;
  44.  
  45. int getState(int state, int i) {
  46. return (state >> (i << )) & ;
  47. }
  48.  
  49. void setState(int &state, int i, int val) {
  50. i <<= ;
  51. state = (state & ~( << i)) | (val << i);
  52. }
  53.  
  54. int solve() {
  55. pre = &hashmap[], cur = &hashmap[];
  56. cur->clear();
  57. cur->insert(, );
  58. int maxState = ( << ((m + ) << )) - ;
  59. for(int i = ; i < n; ++i) {
  60. for(int p = ; p < cur->ecnt; ++p)
  61. cur->to[p] = (cur->to[p] << ) & maxState;
  62. for(int j = ; j < m; ++j) {
  63. swap(pre, cur);
  64. cur->clear();
  65. for(int p = ; p < pre->ecnt; ++p) {
  66. int st = pre->to[p];
  67. for(int k = ; k < ; ++k) {
  68. if(j != && w[(int)s[i][j][(k + ) & ]] != getState(st, j)) continue;
  69. if(i != && w[(int)s[i][j][(k + ) & ]] != getState(st, j + )) continue;
  70. int new_st = st;
  71. setState(new_st, j, w[(int)s[i][j][k]]);
  72. setState(new_st, j + , w[(int)s[i][j][(k + ) & ]]);
  73. cur->insert(new_st, pre->val[p]);
  74. }
  75. }
  76. }
  77. }
  78. int res = ;
  79. for(int p = ; p < cur->ecnt; ++p) {
  80. res += cur->val[p];
  81. if(res >= MOD) res -= MOD;
  82. }
  83. return res;
  84. }
  85.  
  86. int main() {
  87. w['F'] = ; w['C'] = ; w['R'] = ;
  88. scanf("%d", &T);
  89. for(int t = ; t <= T; ++t) {
  90. scanf("%d%d", &n, &m);
  91. for(int i = ; i < n; ++i)
  92. for(int j = ; j < m; ++j) scanf("%s", s[i][j]);
  93. printf("Case %d: %d\n", t, solve());
  94. }
  95. }

HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)的更多相关文章

  1. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  2. HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...

  3. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  4. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. hdu 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878 题目大意: 给出一个数n ,求一个数X, X>=n. X 满足一个条件 X= 2^a*3^ ...

  6. HDU 5437 Alisha’s Party (优先队列)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Princess Alisha invites her friends to come to her birthday party. Each of her f ...

  7. 2014 ACM/ICPC Asia Regional Beijing Site

    1001 A Curious Matt 1002 Black And White 1003 Collision 1004 Dire Wolf 1005 Everlasting L 1006 Fluor ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

随机推荐

  1. [dpdk] 读官方文档(1)

    前提:已读了这本书<<深入浅出dpdk(朱清河等著)>>. 目标:读官方文档,同时跟着文档进行安装编译等工作. http://dpdk.org/doc/guides/index ...

  2. jquery easyui Combobox 实现 两级联动

    具体效果如下图:

  3. easy UI简单使用介绍

    http://www.cnblogs.com/yokoboy/archive/2012/12/06/2806132.html

  4. OO之美4(好代码与坏代码)

    前言:写代码不仅仅要做到能与机器交流,更要做到能和人交流 编码规范:编码规范就是最佳实践,是前辈在编码这件事上的积累和总结,是智慧的延续和工业的实践,如下: ⑴命名规范 ⑵避免行数过多的方法 ⑶代码缩 ...

  5. JMeter学习-005-JMeter 主要组件概要介绍及执行顺序

    本文将对 JMeter 主要组件(主要涉及 Threads(Users).Test Fragment.逻辑控制器.配置元件.定时器.前置处理器.Sampler.后置处理器.断言.监听器 十大组件)进行 ...

  6. 解决在.ashx文件中判断Session 总是NULL的方法

    实现IHttpHandler接口的同时必须继承IRequiresSessionState接口,才能拿到session public class HttpHandler: IHttpHandler, I ...

  7. 在CentOS6.5上安装Tomcat6

    Tomcat安装一向方便,linux的比win的更是这样,基本就是拷贝,类似于win中备受青睐的绿色软件,下面只是记录一下过程. 1.从 http://mirrors.cnnic.cn/apache/ ...

  8. TortoiseGit使用与操作

    使用 Git命令有时候确实不怎么方便,特别是每次都要输入密码,如果配置 SSH 的方式,又实在是很麻烦.(当然,必须使用 Windows 神器才有方便友好的客户端图形界面啦!!!) 1.克隆项目 打开 ...

  9. SpringMVC @ResponseBody的使用

    原文链接:http://www.jianshu.com/p/7097fea8ce3f@ResponseBody用法作用:该注解用于将Controller的方法返回的对象,根据HTTP Request ...

  10. docker-compose常用命令

    --verbose:输出详细信息-f 制定一个非docker-compose.yml命名的yaml文件-p 设置一个项目名称(默认是directory名)docker-compose的动作包括:bui ...