There is a hive in the village. Like this. There are 8 columns(from A to H) in this hive. Different colums have the same number of grids. Every grid has its own coordinate, which is formed by two uppercases, representing the row index and the column index. The row index starts from A. And the hive has less than ten rows in total. The following figure shows a hive with two rows.

There is honey in some grids. A naughty bee discovers this special hive and hopes to eat all honey in the hive. However, this strange bee sets some rules for itself while eating. They are descirbed as following:

  • It must eat the honey by choosing a circuit and then eat all honey that is in the chosen circuit.
  • Honey will disappear immediately after the bee eats it.
  • All grids which are in the circuit should has honey in it.
  • The length of the circuit should be no less than 3.
  • The bee can choose more than one circuit to eat honey.

Given the hive and the honey in it, how many different ways can this naughty bee eat all honey in the hive?

Input

There are multiple test cases.

For each case, there are two integers N(0 < N ≤ 10) and M(0 ≤ M ≤ N * 8) in the first line. N represents the size of the hive, which means there are N rows in the hive in total. All grids have honey in it except for those M grids listed in the following line. Each empty grid is described by its coordinate(using two uppercases).

Output

For each case, output the number of different ways the bee can eat all honey in the hive. It's guaranteed that the answer does not exceed 263 - 1.

Sample Input

  1. 3 5
  2. BB CD BF AH CG

Sample Output

  1. 3

Hint

The following figure shows all different ways for the sample. The black grids represent those which are initially empty.

六边形插头DP诶,好像很兹磁的样子……

码起来其实也不会太麻烦。轮廓线放在列上而不要放在行上会好写很多(其实就是把地图旋转90°),只是可能稍慢。二进制记录有无插头,然后对于一个点的三个入插头就分0,1,2个的情况算,哪一个或者哪两个都是没关系的,3个显然不合法。

然后……只记得我错把n打成m,然后调了很久(我说怎么8 0的时候答案正确,9 0就挂了……)

  1. #include<queue>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define ll long long
  6. #define MMH(a,b,c) up(no.x,gx(2*x,a,b,c,no.z),an)
  7. using namespace std;
  8. const int MAXN=;
  9. struct na{
  10. int x,z;
  11. na(int xx,int zz):x(xx),z(zz){}
  12. };
  13. int n,m=,M,x,y,z,a[],k,en,ln;
  14. bool Q[][];
  15. ll f[][MAXN+],mmh;
  16. int v[][MAXN+];
  17. queue <na> q;
  18. inline int gx(int x,int q1,int q2,int q3,int k){q1^=;q2^=;q3^=;k|=(<<x)|(<<(x+))|(<<(x+));k^=(q1<<x)|(q2<<(x+))|(q3<<(x+));return k;}
  19. inline void up(int x,int z,ll lj){
  20. k=(++x)&;
  21. if (v[k][z]!=x) v[k][z]=x,f[k][z]=,q.push(na(x,z));
  22. f[k][z]=f[k][z]+lj;
  23. }
  24. char c[];
  25. int main(){
  26. register int i,j;
  27. while(~scanf("%d%d",&n,&M)){
  28. mmh=;
  29. memset(Q,,sizeof(Q));
  30. memset(v,,sizeof(v));
  31. for (i=;i<n;i++)
  32. for (j=;j<m;j++) Q[i][j]=;
  33. for (i=;i<M;i++)
  34. scanf("%s",&c),Q[n-(c[]-'A')-][c[]-'A']=;
  35. en=n*m;
  36. while(!Q[en%n][en/n]&&en>) en--;en++;
  37. f[][]=v[][]=;
  38. q.push(na(,));
  39. while(!q.empty()){
  40. na no=q.front();q.pop();
  41. ll an=f[no.x&][no.z];
  42. if (ln!=no.x) ln=no.x,y=ln/n,x=ln-y*n;
  43. if (x==&&(y&)==) no.z<<=;
  44. for (i=,j=no.z;j;i++,j>>=) a[i]=j%;
  45. for (;i<=(n<<);i++) a[i]=;
  46. if (!Q[x][y]){
  47. if (no.x==en) mmh+=an;else
  48. MMH(,,);
  49. }else
  50. if (y&){
  51. if (a[*x]+a[*x+]+a[*x+]==){
  52. if (Q[x][y+]&Q[x+][y+]) MMH(,,);
  53. if (Q[x][y+]&Q[x+][y]) MMH(,,);
  54. if (Q[x+][y+]&Q[x+][y]) MMH(,,);
  55. }else if (a[*x]+a[*x+]+a[*x+]==){
  56. if (Q[x][y+]) MMH(,,);
  57. if (Q[x+][y+]) MMH(,,);
  58. if (Q[x+][y]) MMH(,,);
  59. }else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
  60. }else{
  61. if (a[*x]+a[*x+]+a[*x+]==){
  62. if (x) if (Q[x-][y+]&Q[x][y+]) MMH(,,);
  63. if (x) if (Q[x-][y+]&Q[x+][y]) MMH(,,);
  64. if (Q[x][y+]&Q[x+][y]) MMH(,,);
  65. }else if (a[*x]+a[*x+]+a[*x+]==){
  66. if (x) if (Q[x-][y+]) MMH(,,);
  67. if (Q[x][y+]) MMH(,,);
  68. if (Q[x+][y]) MMH(,,);
  69. }else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
  70. }
  71. }
  72. printf("%llu\n",mmh);
  73. }
  74. }

1490MS 52920KB

ZOj 3466 The Hive II的更多相关文章

  1. ZOJ 3466 The Hive II (插头DP,变形)

    题意:有一个n*8的蜂房(6边形的格子),其中部分是障碍格子,其他是有蜂蜜的格子,每次必须走1个圈取走其中的蜂蜜,在每个格子只走1次,且所有蜂蜜必须取走,有多少种取法? 思路: 以前涉及的只是n*m的 ...

  2. zoj 3620 Escape Time II dfs

    题目链接: 题目 Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 问题描述 There is a fire in LTR ' s home ...

  3. hive(II)--sql考查的高频问题

    在了解别人hive能力水平的时候,不管是别人问我还是我了解别人,有一些都是必然会问的东西.问的问题也大都大同小异.这里总结一下我遇到的那些hive方面面试可能涉及的问题 1.行转列(列转行) 当我们建 ...

  4. zoj 3356 Football Gambling II【枚举+精度问题】

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...

  5. zoj 3620 Escape Time II

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...

  6. ZOJ 3332 Strange Country II

    Strange Country II Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge You want to v ...

  7. ZOJ 3042 City Selection II 【序】【离散化】【数学】

    题意: 输入数据n,m.n代表工厂的数量,m代表城市的数量. 接下来n+m行为工厂和城市的坐标. 规定如图所示方向刮风,工厂的air会污染风向地区的air. 注意,工厂和城市的坐标表示的是从x到x+1 ...

  8. zoj 3627 Treasure Hunt II (贪心)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的.  每个城市都有vi的金币.   ...

  9. ZOJ3466-The Hive II

    题意 有一个六边形格子,共 \(n\) 行,每行有 8 个位置,有一些格子不能走.求用一些环覆盖所有可走格子的方案数.\(n\le 10\) . 分析 插头dp,只不过是六边形上的,分奇数列和偶数列讨 ...

随机推荐

  1. 【十九】require和include的区别

    1.require是一个函数,include是一个关键字 2.require是无返回值,include有返回值 3.include()会产生一个警告,而require()则导致一个致命的错误(出现错误 ...

  2. iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD

    原文:http://www.cocoachina.com/ios/20160919/17595.html DEMO链接

  3. Kafka+Storm写入Hbase和HDFS

    1.Storm整合Kafka 使用Kafka作为数据源,起到缓冲的作用 // 配置Kafka订阅的Topic,以及zookeeper中数据节点目录和名字 String zks = KafkaPrope ...

  4. kvm 随笔

    1. 查看kvm虚拟机状态 # virsh list --all   2. KVM虚拟机开机 # virsh start windows   3. KVM虚拟机关机或断电 (1) 关机 virsh关机 ...

  5. python写入文本报错TypeError: expected a string or other character buffer object

    今天用python写入文本, file_object2 = open('result.txt', 'w') file_object2.write(bookid_list) file_object2.c ...

  6. WPF-控件(1)

    小记:最近公司使用WPF开发一款新的产品,从零基础开始学习WPF,在看书的过程中,有些内容需要规整到一起,最终选择了博客园,希望对自己所掌握的知识有所规整. --------------------- ...

  7. 使用plenv安装perl,并使其支持多线程

    plenv与pyenv.rbenv等都是同类型软件中非常好用的,这三个软件不仅命名类似,操作方式也相差无几,节约了很多学习的成本,所以非常推荐: 安装使用plenv: git clone git:// ...

  8. 【JDK1.8】JDK1.8集合源码阅读——LinkedList

    一.前言 这次我们来看一下常见的List中的第二个--LinkedList,在前面分析ArrayList的时候,我们提到,LinkedList是链表的结构,其实它跟我们在分析map的时候讲到的Link ...

  9. Javascript流程控制

    Javascript流程控制 1.条件语句 (1)if(exp)执行一句代码 (2)if(exp){执行代码段;} (3)if(exp){exp为true执行代码段}else{exp为false执行的 ...

  10. idea创建Maven多模块项目

    最近几天学习到了创建多模块项目,应为自己使用的是Idea,所以想用idea创建多模块,查阅了相关资料后,自己做一个记录. 一.首先创建一个maven项目 Parent Project,创建xxx-ro ...