The Hive II
Time Limit: 5 Seconds Memory Limit: 65536 KB
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.
Author: MO, Luyi
Contest: ZOJ Monthly, January 2011
分析:此题为连通性dp(插头DP),可以横着dp,也可以竖着dp,竖着来会超时,因为n<=10>8。。。不管怎样,状态转移都差不多,用二进制表示每个插头的状态,如图
黑色箭头表示所有的插头,每一行转移都先转移在上面的BDFH,即偶数列,每一格控制三个插头的状态,用8进制表示,插头的状态由上面三个黑色转移到下面三个红色,ACEG的转移类似。。。
把状态的连接保存在数组中,转移时直接枚举然后用位移计算相应的位置,并计算状态。。。
(000)<->(101,110,011) (100,010,001)<->(100,010,001) 这些状态互相转化。。。
代码(写得好挫,不过还是可以AC的):
竖着来写比较简单,方法类似横着,不过因为这题数据原因,只能超时了。。。: