bzoj 1814 Ural 1519 Formula 1 插头DP

时间:2024-11-07 19:37:08

1814: Ural 1519 Formula 1

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 942  Solved: 356
[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

题解:什么叫做哈密顿回路,就是从一个点出发,经过所有点一次仅一次,

所有可以知道路不能交叉。

具体还是看cdqhttps://wenku.baidu.com/view/9cfbb16e011ca300a6c390d5.html

 #pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring> #define sz 200005
#define ll long long
using namespace std; int hs[sz],tot[],sta[][sz],bit[];
int n,m,c,ex,ey,mp[][];
ll dp[][sz],ans;
char s[]; void add(int s,ll num)
{
int pos=s%sz;
while (hs[pos]!=-)
{
if (sta[c][hs[pos]]==s)
{
dp[c][hs[pos]]+=num;
return;
}
pos++;
if (pos==sz) pos=;
}
dp[c][++tot[c]]=num;
hs[pos]=tot[c];
sta[c][tot[c]]=s;
}
void DP()
{
for (int i=;i<=n;i++)
{
for (int k=;k<=tot[c];k++)
sta[c][k]<<=;
for (int j=;j<=m;j++)
{
c^=; tot[c]=;
memset(hs,-,sizeof(hs));
for (int k=;k<=tot[c^];k++)
{
int s=sta[c^][k];
ll num=dp[c^][k];
int p=(s>>bit[j-])&,q=(s>>bit[j])&;
if (!mp[i][j])
{
if (!p&&!q) add(s,num);
}
else if (!p&&!q)
{
if (!mp[i+][j]||!mp[i][j+]) continue;
s+=(<<bit[j-])+(<<(bit[j]+)) ;
add(s,num);
}
else if (!p&&q){
if (mp[i][j+]) add(s,num);
if (mp[i+][j]){
s+=(<<bit[j-])*q-(<<bit[j])*q;
add(s,num);
}
}
else if (!q&&p){
if (mp[i+][j]) add(s,num);
if (mp[i][j+]){
s+=(<<bit[j])*p-(<<bit[j-])*p;
add(s,num);
}
}
else if (p+q==){
int b=;
for (int t=j+;t<=m;t++){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s-=(<<bit[t]);
break;
}
}
s-=(<<bit[j-])+(<<bit[j]);
add(s,num);
}
else if (p+q==){
int b=;
for (int t=j-;t>=;t--){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s+=(<<bit[t]);
break;
}
}
s-=*(<<bit[j-])+*(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
s-=(<<(bit[j-]+))+(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
if (i==ex&&j==ey) ans+=num;
}
}
}
}
}
int main(){
for (int i=;i<=;i++)
bit[i]=i<<;
scanf("%d%d",&n,&m);
tot[]=; dp[c][]=;
for (int i=;i<=n;i++){
scanf("%s",s+);
for (int j=;j<=m;j++)
if (s[j]=='.') mp[i][j]=,ex=i,ey=j;
}
DP();
printf("%lld\n",ans);
}

那么对于这道题目,分析其特殊性质。