1814: Ural 1519 Formula 1

1814: Ural 1519 Formula 1

Time Limit: 1 Sec   Memory Limit: 64 MB
Submit: 699   Solved: 249
[ Submit][ Status][ Discuss]

Description

Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. Problem Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said. It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here. 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2^63-1.

Sample Input

4 4
**..
....
....
....



Sample Output


2

HINT

Source

[ Submit][ Status][ Discuss]

学了下连通性dp,具体可见CDQ2008年的论文,此处补点笔记
因为不同的连通块之间不会交叉,所以可以用括号序列表示轮廓线上的插头
转移的话,,按照轮廓线上行第一列和下行最后一列的右部,分类讨论
特别的,当这两个刚好组成(),只能在最后一行最后一个位置合法,因为这个相当于是结束路径
时限1s,,跑了1.3s。。。醉
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long LL;
const int N = 1594323;

LL f[2][N],mi[14];
int n,m,ex,ey,now = 1,nex;
bool vis[2][N];
char s[13][13];

queue <int> Q[2];

int Get(const int &x,const int &y) {return x / mi[y] % 3;}
void Val(int &k,const int &x,const int &y) {k += (y - Get(k,x))*mi[x];}
void Left(int &x) {x *= 3; Val(x,m + 1,0);}

int Find2(const int &k,const int &g)
{
	int tot = 0;
	for (int i = g; ; i--)
	{
		int t = Get(k,i);
		if (t == 2) {--tot; if (!tot) return i;}
		else if (t == 1) ++tot;
	}
}

int Find1(const int &k,const int &g)
{
	int tot = 0;
	for (int i = g; ; i++)
	{
		int t = Get(k,i);
		if (t == 1) {--tot; if (!tot) return i;}
		else if (t == 2) ++tot;
	}
}

void Push(const int &k,const int &nk)
{
	f[nex][nk] += f[now][k];
	if (!vis[nex][nk]) vis[nex][nk] = 1,Q[nex].push(nk);
}

void push_all()
{
	while (!Q[now].empty())
	{
		int k = Q[now].front(),nk = k; Q[now].pop();
		int head = Get(k,m),tail = Get(k,0);
		if (!head && !tail) Left(nk),Push(k,nk);
		f[now][k] = vis[now][k] = 0;
	}
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m; mi[0] = 1;
	for (int i = 1; i <= m + 1; i++) mi[i]= mi[i-1]*3;
	for (int i = 1; i <= n; i++) scanf("%s",s[i] + 1);
	for (int i = n; i; i--)
	{
		bool pass = 0;
		for (int j = m; j; j--)
			if (s[i][j] != '*')
			{
				ex = i; ey = j;
				pass = 1; break;
			}
		if (pass) break;
	}
	vis[0][0] = f[0][0] = 1; Q[0].push(0);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
		{
			now ^= 1; nex ^= 1;
			if (s[i][j] == '*') {push_all(); continue;}
			while (!Q[now].empty())
			{
				int k = Q[now].front(),nk = k; Q[now].pop();
				int head = Get(k,m),tail = Get(k,0);
				if (head && tail)
				{
					if (j != 1)
					{
						if (head == 1 && tail == 1)
						{
							int pos = Find2(nk,m);
							Val(nk,m,0); Val(nk,0,0);
							Val(nk,pos,1); Left(nk); Push(k,nk);
						}
						else if (head == 2 && tail == 2)
						{
							int pos = Find1(nk,0);
							Val(nk,0,0); Val(nk,m,0);
							Val(nk,pos,2); Left(nk); Push(k,nk);
						}
						else if (head == 2 && tail == 1)
						{
							if (i == ex && j == ey)
							{
								Val(nk,0,0); Val(nk,m,0);
								Left(nk); Push(k,nk);
							}
						}
						else 
						{
							Val(nk,0,0); Val(nk,m,0);
							Left(nk); Push(k,nk);
						}
					}
				}
				else if (!head && !tail)
				{
					if (j != m)
					{
						Left(nk); Val(nk,0,2);
						Val(nk,1,1); Push(k,nk);
					}
				}
				else if (head)
				{
					Val(nk,m,0); Left(nk); 
					Val(nk,1,head); Push(k,nk);
					if (j != m)
					{
						nk = k; Val(nk,m,0); Left(nk); 
						Val(nk,0,head); Push(k,nk);
					}
				}
				else
				{
					Val(nk,0,0); Left(nk); 
					Val(nk,1,tail); Push(k,nk);
					if (j != m)
					{
						nk = k; Val(nk,0,0); Left(nk); 
						Val(nk,0,tail); Push(k,nk);
					}
				}
				f[now][k] = vis[now][k] = 0;
			}
		}
	cout << f[nex][0];
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值