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

3 5
BB CD BF AH CG

Sample Output

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就挂了……)

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define MMH(a,b,c) up(no.x,gx(2*x,a,b,c,no.z),an)
using namespace std;
const int MAXN=;
struct na{
int x,z;
na(int xx,int zz):x(xx),z(zz){}
};
int n,m=,M,x,y,z,a[],k,en,ln;
bool Q[][];
ll f[][MAXN+],mmh;
int v[][MAXN+];
queue <na> q;
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;}
inline void up(int x,int z,ll lj){
k=(++x)&;
if (v[k][z]!=x) v[k][z]=x,f[k][z]=,q.push(na(x,z));
f[k][z]=f[k][z]+lj;
}
char c[];
int main(){
register int i,j;
while(~scanf("%d%d",&n,&M)){
mmh=;
memset(Q,,sizeof(Q));
memset(v,,sizeof(v));
for (i=;i<n;i++)
for (j=;j<m;j++) Q[i][j]=;
for (i=;i<M;i++)
scanf("%s",&c),Q[n-(c[]-'A')-][c[]-'A']=;
en=n*m;
while(!Q[en%n][en/n]&&en>) en--;en++;
f[][]=v[][]=;
q.push(na(,));
while(!q.empty()){
na no=q.front();q.pop();
ll an=f[no.x&][no.z];
if (ln!=no.x) ln=no.x,y=ln/n,x=ln-y*n;
if (x==&&(y&)==) no.z<<=;
for (i=,j=no.z;j;i++,j>>=) a[i]=j%;
for (;i<=(n<<);i++) a[i]=;
if (!Q[x][y]){
if (no.x==en) mmh+=an;else
MMH(,,);
}else
if (y&){
if (a[*x]+a[*x+]+a[*x+]==){
if (Q[x][y+]&Q[x+][y+]) MMH(,,);
if (Q[x][y+]&Q[x+][y]) MMH(,,);
if (Q[x+][y+]&Q[x+][y]) MMH(,,);
}else if (a[*x]+a[*x+]+a[*x+]==){
if (Q[x][y+]) MMH(,,);
if (Q[x+][y+]) MMH(,,);
if (Q[x+][y]) MMH(,,);
}else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
}else{
if (a[*x]+a[*x+]+a[*x+]==){
if (x) if (Q[x-][y+]&Q[x][y+]) MMH(,,);
if (x) if (Q[x-][y+]&Q[x+][y]) MMH(,,);
if (Q[x][y+]&Q[x+][y]) MMH(,,);
}else if (a[*x]+a[*x+]+a[*x+]==){
if (x) if (Q[x-][y+]) MMH(,,);
if (Q[x][y+]) MMH(,,);
if (Q[x+][y]) MMH(,,);
}else if (!(a[*x]&a[*x+]&a[*x+])) MMH(,,);
}
}
printf("%llu\n",mmh);
}
}

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. 【java】泛型的作用是在编译阶段防止错误输入,绕过编译就绕过泛型,可用反射验证

    package com.tn.collect; import java.lang.reflect.Method; import java.util.ArrayList; public class Fa ...

  2. 关于ubuntu下qt编译显示Cannot connect creator comm socket /tmp/qt_temp.xxx/stub-socket的解决办法

    今天在ubuntu下安装了qtcreator,准备测试一下是否能用,果然一测试就出问题了,简单编写后F5编译在gnome-terminal中出现 Cannot connect creator comm ...

  3. http头部 Expect

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/90 在通过curl调用对方接口时,发现超时现象很严重,于是询问对 ...

  4. Linux_异常_08_本机无法访问虚拟机web等工程

    这是因为防火墙的原因,把响应端口开启就行了. # Firewall configuration written by system-config-firewall  # Manual customiz ...

  5. tar: This does not look like a tar archive tar: Skipping to next header tar: Exiting with failure status due to previous errors

    解压一个.tar.zip文件时报错 tar -zxvf bcl2fastq2-v2---linux-x86-.zip tar: This does not look like a tar archiv ...

  6. [编织消息框架][netty源码分析]12 ByteBuf 实现类UnpooledDirectByteBuf职责与实现

    public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { private final ByteBufAl ...

  7. Webpack 2 视频教程 016 - Webpack 2 中生成 SourceMaps

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  8. Git详解之六:Git工具

    Git 工具 现在,你已经学习了管理或者维护 Git 仓库,实现代码控制所需的大多数日常命令和工作流程.你已经完成了跟踪和提交文件的基本任务,并且发挥了暂存区和轻量级的特性分支及合并的威力.(伯乐在线 ...

  9. ABP .Net Core 日志组件集成使用NLog

    一.说明 NLog介绍和使用说明官网:http://nlog-project.org/ NLog和Log4net对比:https://www.cnblogs.com/qinjin/p/5134982. ...

  10. C# DataGridView 列的显示顺序

    this.dataGridView1.Columns["列名"].DisplayIndex=Convert.ToInt32("你要放置的位置")