您的位置:首页 > 其它

URAL1519 Formula 1

2017-02-23 20:53 197 查看

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 NM 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 NM = 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.

Input

The first line contains the integer numbers N and M (2 ≤ NM ≤ 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.

Example

inputoutput
4 4
**..
....
....
....
2
4 4
....
....
....
....
6

 

 

 

这算完全体的插头DP吧?

果然超麻烦

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int mod=100007;
const int mxn=650010;
struct edge{
int v,nxt,id;
}e[2][mxn];
int hd[2][mod+2],mct[2];
LL f[2][mxn];
int now,pre;
void add(int v,LL w){//hash
int z=v%mod;
for(int i=hd[now][z];i;i=e[now][i].nxt){
if(v==e[now][i].v){f[now][i]+=w;return;}
}
e[now][++mct[now]]=(edge){v,hd[now][z],z};hd[now][z]=mct[now];
f[now][mct[now]]=w;
return;
}
int b[20];
int n,m;
char mp[20][20];
int main(){
int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%s",mp[i]+1);
int ex,ey;
for(i=n;i;i--){
for(j=m;j;j--)if(mp[i][j]=='.'){ex=i;ey=j;break;}
if(j)break;
}
for(i=1;i<=m;i++)b[i]=i<<1;
now=0;pre=1;
add(0,1);
LL ans=0;
for(i=1;i<=n;i++){
for(int k=1;k<=mct[now];k++)e[now][k].v<<=2;
for(j=1;j<=m;j++){
swap(now,pre);
for(int k=1;k<=mct[now];k++)hd[now][e[now][k].id]=0;
mct[now]=0;
//init
//            printf("i:%d j:%d mp:%c\n",i,j,mp[i][j]);
for(int k=1;k<=mct[pre];k++){
int v=e[pre][k].v;
int x=(v>>b[j-1])&3;
int y=(v>>b[j])&3;
LL w=f[pre][k];
//                printf("%d %d k:%d v:%d w:%I64d\n",i,j,k,v,w);
//                printf("x:%d  y:%d\n",x,y);
if(mp[i][j]=='.'){
if(!(x+y)){
if(mp[i+1][j]=='.' && mp[i][j+1]=='.')
add(v^(1<<b[j-1])^(2<<b[j]),w);
}
else if(!x && y==1){
if(mp[i+1][j]=='.')add(v^(1<<b[j-1])^(1<<b[j]),w);
if(mp[i][j+1]=='.')add(v,w);
}
else if(!x && y==2){
if(mp[i+1][j]=='.')add(v^(2<<b[j-1])^(2<<b[j]),w);
if(mp[i][j+1]=='.')add(v,w);
}
else if(x==1 && !y){
if(mp[i][j+1]=='.')add(v^(1<<b[j-1])^(1<<b[j]),w);
if(mp[i+1][j]=='.')add(v,w);
}
else if(x==1 && y==1){// (( 如果右边的左右括号能匹配,这两个可以接起来
int cnt=1;
for(int c=j+1;c<=m;c++){
int tmp=(v>>b[c])&3;
if(tmp==1)cnt++;
if(tmp==2)cnt--;//不能else
if(!cnt){add((v^(1<<b[j-1])^(1<<b[j]))-(1<<b[c]),w);break;}
}

}
else if(x==1 && y==2){//只有在终点才能这么合并
if(i==ex && j==ey)ans+=w;
}
else if(x==2 && !y){
if(mp[i][j+1]=='.')add(v^(2<<b[j-1])^(2<<b[j]),w);
if(mp[i+1][j]=='.')add(v,w);
}
else if(x==2 && y==1){
add(v^(2<<b[j-1])^(1<<b[j]),w);
}
else if(x==2 && y==2){
int cnt=-1;
for(int c=j-2;c>=0;c--){
int tmp=(v>>b[c])&3;
if(tmp==1)cnt++;
if(tmp==2)cnt--;
if(!cnt){add((v^(2<<b[j-1])^(2<<b[j]))+(1<<b[c]),w);break;}
}
}
}
else{if(!(x+y))add(v,w);}
}
}
}
printf("%I64d\n",ans);
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: