http://poj.org/problem?id=1739
http://www.acmsearch.com/article/show/17209
这题很容易想到把左下和右下两个格子连起来,构成一条哈密顿回路,就和上一题一样,不过这条路有一条必须经过的路径,我们需要特殊处理,做法有两种,我是添加了一层,然后特殊处理最后一层,后来发现小hh的解法说不用,又写了另一种解法,就是将最后一层的状态变成初始状态,然后从底往上dp,这种写法比较好,简单,而且状态比第一种写法少。
A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked. The Farm is located in the lower left plot and the Market is located in the lower right plot. Tony takes her tour of the township going from Farm to Market by walking through every unblocked plot exactly once.
Write a program that will count how many unique tours Betsy can take in going from Farm to Market.
Write a program that will count how many unique tours Betsy can take in going from Farm to Market.
Input
The input contains several test cases. The first line of each test case contain two integer numbers n,m, denoting the number of rows and columns of the farm. The following n lines each contains m characters, describe the farm. A '#' means a blocked square, a '.' means a unblocked square.
The last test case is followed by two zeros.
The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
2 2 .. .. 2 3 #.. ... 3 4 .... .... .... 0 0
Sample Output
1 1 4
这是第一次写这样的插头DP,状态转移有点复杂。主要参考了http://blog.csdn.net/xingyeyongheng/article/details/24415517
不过还没了解什么是插头DP的话,建议先仔细认真的看陈丹琦的《基于连通性状态压缩的动态规划问题》https://wenku.baidu.com/view/a6dce6c76137ee06eff918d1.html
这题除了一开始的预处理,基本上就是插头的模板题了
由于插头求的是回路,而此题有起点和终点的限制
于是可以构造一条的路径,正好只添加一条的路径
接下来就是插头的模板了
推荐三篇文章,看完基本上就懂插头了吧,
yhzq:远航之曲博客
可以发现,插头其实就是对于当前已枚举部分和未枚举部分的轮廓线的状压
注意每枚举过一行要将所有状态左移一位(下一行会多出来一个状态位)
插头DP。题目意思就是从左下角走到右下角,每个非障碍格子都走一遍的方法数。
一种方法是后面加两行转化成回路问题。
也可以不增加行,只要在起点和终点特殊处理下即可。
这题很容易想到把左下和右下两个格子连起来,构成一条哈密顿回路,就和上一题一样,不过这条路有一条必须经过的路径,我们需要特殊处理,做法有两种,我是添加了一层,然后特殊处理最后一层,后来发现小hh的解法说不用,又写了另一种解法,就是将最后一层的状态变成初始状态,然后从底往上dp,这种写法比较好,简单,而且状态比第一种写法少。