题目链接:http://acm.fzu.edu.cn/problem.php?pid=1977
题意:地图上有三种点,不可走点,必须走的点,可走可不走的点。问有多少种单回路,可以经过所以的必须走的点。
思路:插头dp,分三种类型格子讨论转移。由于回路的终点不固定,所以先记录最后一个可行格子的坐标,如果当前位置不小于最后一个可行格子,且合并相同连通性的格子形成单回路,我们就累加方案数,并且此状态不继续转移。
#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;
int edx , edy;
char g[20][20];
const int maxn = 109999;
LL ans;
int bit = 7;
int inc = 3;
int code[20];
int vis[20];
struct hash_table
{
int head[10007] , 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 % 10007;
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];
LL S = dp[pre].state[k];
code[y] = code[y-1] = 0;
if ( g[x][y] == 'X' || g[x][y] == '*' ) //不可走 无插头=》无插头
{
if ( !left && !up ) dp[cur].push( S , V );
}
if ( g[x][y] == 'O' || g[x][y] == '*' ) //必须走 且一个格子有固定两个插头
{
if ( !left && !up ) //新增两个连通插头
{
if ( x < n && g[x+1][y] != 'X' && y < m && g[x][y+1] != 'X' )
{
code[y] = code[y-1] = bit;
dp[cur].push( encode(m) , V );
}
}
else if ( !left || !up ) //新增一个插头
{
if ( x < n && g[x+1][y] != 'X' ) //下插头
code[y-1] = left + up , dp[cur].push( encode(m) , V ) , code[y-1] = 0;
if ( y < m && g[x][y+1] != 'X' ) //右插头
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 );
}
else if ( ( x > edx || (x==edx && y >= edy) ) && (left == up) && check(m) ) //除了left和up位置,其他地方均无插头 才可形成回路
ans += 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 );
}
}
return ans;
}
void init()
{
ans = edx = edy = 0;
scanf("%d%d",&n,&m);
getchar();
for( int i = 1; i <= n; i++ )
{
gets( g[i] + 1 );
for( int j = 1; j <= m; j++ )
if( g[i][j] == 'O' ) edx = i , edy = j;
}
}
int main()
{
int T;
cin>>T;
for( int cas = 1; cas <= T; cas++ )
{
init();
printf("Case %d: %I64d\n",cas,solve());
}
return 0;
}