题目链接:http://poj.org/problem?id=1739
题意:求从左下角走到右下角,每个非障碍格子只走一次的方案数。
思路:求简单路径的数量,这个简单路径起点和终点固定,其次要把所有的非障碍格子都访问到。所以分为起点、终点、障碍格子和一般格子,一般格子有两个插头,且不能形成回路,即不能合并已经连通的插头。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define Clean(x,y) memset(x,y,sizeof(x))
int n,m;
int pre,cur;
char g[20][20];
const int Hash = 10007;
const int maxn = 509999;
LL ans;
int bit = 7;
int inc = 3;
int code[20];
int vis[20];
struct hash_table
{
int head[Hash] , next[maxn];
LL state[maxn],value[maxn];
int size;
void clear()
{
size = 0;
Clean(head,-1);
}
void push( LL S , LL V )
{
int index = S % Hash;
for( int k = head[index]; k != -1; k = next[k] )
if ( state[k] == S )
{
value[k] += V;
return;
}
state[size] = S , value[size] = V;
next[size] = head[index] , head[index] = size++;
}
}dp[2];
inline void decode( LL S , int m )
{
for( int i = 0; i <= m; i++ ) code[i] = S & bit , S >>= inc;
}
inline LL encode( int m )
{
LL ans = 0;
int now = 1;
Clean(vis,-1);
vis[0] = 0;
for( int i = m; i >= 0; i-- )
{
if ( -1 == vis[ code[i] ] ) vis[code[i]] = now++;
code[i] = vis[ code[i] ];
ans <<= inc;
ans |= code[i];
}
return ans;
}
bool check( int m )
{
for( int i = 0; i <=m; i++ ) if ( code[i] ) return false;
return true;
}
void DP( int x , int y , int k ) //0???? 1????? 2??2? 3??3?
{
decode( dp[pre].state[k] , m );
int left = code[y-1] , up = code[y];
LL V = dp[pre].value[k];
code[y] = code[y-1] = 0;
if ( g[x][y] == '#' ) //障碍格子 不能有插头
{
if ( !left && !up ) dp[cur].push( encode(m) , V );
}
else
{
if( x == n && y == 1 ) //起点 有单插头 向上或向右
{
if ( !left )
{
if ( up ) dp[cur].push( encode(m) , V );
else code[y] = bit , dp[cur].push( encode(m) , V );
}
}
else if ( x == n && y == m ) //终点
{
if ( ( left && !up ) || ( !left && up ) ) //单插头
{
dp[cur].push( encode(m) , V );
}
}
else
{
if ( !left && !up )
{
if ( x < n && y < m && g[x+1][y] == '.' && g[x][y+1] == '.' )
code[y-1] = code[y] = bit , dp[cur].push( encode(m) , V );
}
else if ( !left || !up )
{
if ( x < n && g[x+1][y] == '.' )
code[y-1] = left + up , dp[cur].push( encode(m) , V );
code[y-1] = 0;
if ( y < m && g[x][y+1] == '.' )
code[y] = left + up , dp[cur].push( encode(m) , V );
}
else if ( left != up ) //因为是简单路径 , 所以不能形成回路
{
for( int i = 0; i <= m; i++ )
if ( code[i] == left ) code[i] = up;
dp[cur].push( encode(m) , V );
}
}
}
}
LL solve()
{
cur = 0;
dp[0].clear();
dp[0].push( 0 , 1 );
for( int i = 1; i <= n; i++ )
{
pre = cur , cur ^= 1;
dp[cur].clear();
for( int k = 0; k < ( dp[pre].size ); k++ )
dp[cur].push( dp[pre].state[k]<<inc , dp[pre].value[k] );
for( int j = 1; j <= m; j++ )
{
pre = cur , cur ^= 1 , dp[cur].clear();
for( int k = 0; k < dp[pre].size; k++ ) DP( i , j , k );
}
}
for( int k = 0; k < dp[cur].size; k++ )
if ( dp[cur].state[k] == 0 ) return dp[cur].value[k];
return 0;
}
void init()
{
getchar();
for( int i = 1; i <= n; i++ )
{
gets( g[i] + 1 );
}
}
int main()
{
while( scanf("%d%d",&n,&m) == 2 )
{
if ( n + m == 0 ) break;
init();
printf("%lld\n",solve());
}
return 0;
}