您的位置:首页 > 其它

ural1519 Formula 1

2017-02-24 18:24 309 查看
Background

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.

Problem illustration 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.

There are at least 4 cells without gopher holes. Output

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

插头dp,括号序列维护连通性。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long
const int p=233333;
int clo,n,m,i,j;
LL ans;
char map[15][15];
struct res
{
int fir[240010],ne[2000010],val[2000010],last[240010],tot,k,l;
LL ans[2000010];
void init()
{
tot=0;
}
int find(int x)
{
int y=x%p;
if (last[y]<clo)
{
last[y]=clo;
fir[y]=0;
}
for (int i=fir[y];i;i=ne[i])
if (val[i]==x) return i;
ne[++tot]=fir[y];
fir[y]=tot;
val[tot]=x;
ans[tot]=0;
return tot;
}
void upd(int x,LL y)
{
if (j==m-1)
{
if ((x>>2*m)&3) return;
x<<=2;
}
int z=find(x);
ans[z]+=y;
}
int next(LL &x)
{
if (ne[l])
{
l=ne[l];
x=ans[l];
return val[l];
}
for (k++;k<p;k++)
if (last[k]==clo-1)
for (l=fir[k];l;l=ne[l])
{
x=ans[l];
return val[l];
}
return -1;
}
int first(LL &x)
{
for (k=0;k<p;k++)
if (last[k]==clo-1)
for (l=fir[k];l;l=ne[l])
{
x=ans[l];
return val[l];
}
}
}dp[2];
int find_next(int x,int p)
{
int cnt=0,tem;
for (int i=p+1;;i++)
{
tem=(x>>2*i)&3;
if (tem==1) cnt++;
if (tem==2)
{
if (!cnt) return i;
cnt--;
}
}
}
int find_pre(int x,int p)
{
int cnt=0,tem;
for (int i=p-1;;i--)
{
tem=(x>>2*i)&3;
if (tem==1)
{
if (!cnt) return i;
cnt--;
}
if (tem==2) cnt++;
}
}
void pause()
{
int i;
i=1;
}
int main()
{
int lx,ly,x,x1,w,p,q,flag;
LL v;
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
scanf("%s",map[i]);
if (n<m)
{
for (int i=0;i<m;i++)
for (int j=i+1;j<m;j++)
swap(map[i][j],map[j][i]);
swap(n,m);
}
flag=1;
for (i=n-1;i>=0&&flag;i--)
for (j=m-1;j>=0&&flag;j--)
if (map[i][j]=='.')
{
lx=i;
ly=j;
flag=0;
}
dp[0].init();
dp[0].upd(0,1);
clo=0;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
{
clo++;
dp[clo&1].init();
//if (i==3&&j==1) pause();
for (x=dp[clo&1^1].first(v);x>=0;x=dp[clo&1^1].next(v))
{
p=(x>>2*j)&3;
q=(x>>2*(j+1))&3;
x1=x^(p<<2*j)^(q<<2*(j+1));
if (map[i][j]=='*')
{
if (p+q==0) dp[clo&1].upd(x,v);
continue;
}
if (p+q==0) dp[clo&1].upd(x|(1<<2*j)|(2<<2*(j+1)),v);
else
{
if (p*q==0)
{
dp[clo&1].upd(x1|(p+q<<2*j),v);
dp[clo&1].upd(x1|(p+q<<2*(j+1)),v);
}
else
{
if (p==1)
{
if (q==1)
{
w=find_next(x,j+1);
dp[clo&1].upd(x1^(3<<2*w),v);
}
else
{
if (i==lx&&j==ly&&!x1) ans+=v;
}
}
else
{
if (q==1) dp[clo&1].upd(x1,v);
else
{
w=find_pre(x,j);
dp[clo&1].upd(x1^(3<<2*w),v);
}
}
}
}
}
}
cout<<ans<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  插头dp 括号序列