题目链接: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());
}
}

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. JAVA内省(Introspector)

    什么是Java内省:内省是Java语言对Bean类属性.事件的一种缺省处理方法. Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦:所以sun公 ...

  2. Delphi 缩放图像代码 - 支持PNG透明通道

    要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...

  3. 如何将cmd中命令输出保存为TXT文本文件

    在使用Windows 中的cmd.exe工具时,有时候我们想要把我们的输入命令及结果保存起来, 但是用复制的方法过于麻烦:有时输出数据条数过大,会造成内容自动滚出屏幕,无法阅读,我们可将命令运行的结果 ...

  4. 【Android开发学习笔记】【第九课】重力感应

    概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...

  5. QComboBox

    #include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...

  6. C#中jQuery Ajax实例(二)

    上一篇写了一个简单的Ajax异步程序,这一次同样是简单的程序,只不过这次先把参数传到一般处理程序(后缀为ashx)中,再把结果传回到页面. 1.html代码: <html xmlns=" ...

  7. raspberryPi 拍照

    调用python的库,学习raspberryPi的摄像头操作方法. 参考链接: https://www.raspberrypi.org/learning/getting-started-with-pi ...

  8. extern 和 static和 今天的一些代码,12-03

    这是关于标识符的链接属性的,链接属性只有三种:external, internal, none 改变规则: 3.1 文件作用域的变量和函数定义,即在所有 代码块和参数列表之外的标识符,使用static ...

  9. js 事件监听封装

    var eventUtil={//添加句柄 //element,节点 //type,事件类型 //handler,函数 addHandler:function(element,type,handler ...

  10. SqlServer基础:IsNull

    SELECT  @temp = ISNULL(point, 0) FROM   dbo.User where Nid=6 如果User表中的point字段为null的话,则对@temp赋值0