1519. Formula 1
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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.
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.
Samples
input | output |
---|---|
4 4 **.. .... .... .... | 2 |
4 4 .... .... .... .... | 6 |
思路:轮廓线DP。啥都不说了,反正也说不清楚。。看代码吧。。能看懂一点是一点
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX=300000+10;
int head[MAX],nex[MAX],Hash[MAX],Size; //hash链表
long long d[2][MAX],now,pre; //二维滚动数组
long long tp[2][MAX]; //记录状态
long long total[2]; //记录状态总数
int ex,ey; //记录最后一个非'#'的格子
int n,m;
char s[15][15];
void Insert(int x,long long sum) //插入x这个状态,此时方法为sum种
{
int pos=x%MAX;
for(int i=head[pos];i!=-1;i=nex[i])
{
if(x==tp[now][Hash[i]]) //找到相同状态
{
d[now][Hash[i]]+=sum;
return;
}
}
total[now]++; //没找到相同状态就插入x
tp[now][total[now]]=x;
d[now][total[now]]=sum;
Hash[Size]=total[now];
nex[Size]=head[pos];
head[pos]=Size++;
}
int main()
{
while(cin>>n>>m)
{
for(int i=0;i<n;i++)scanf("%s",s[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)if(s[i][j]=='.')ex=i,ey=j;
}
memset(d,0,sizeof d);
memset(head,-1,sizeof head);
memset(total,0,sizeof total);
Size=0;
pre=0,now=1;
d[0][1]=1;
tp[0][1]=0;
total[0]=1;
long long ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int k=1;k<=total[pre];k++)
{
int state=tp[pre][k];
int left=state&3; //左插头状况(0表示没有插头,1表示右括号插头,2表示左括号插头)
int up=state&((1<<(2*m+1))+(1<<(2*m))); //上插头状况(0表示没有插头,1<<(2*m)表示右括号插头,1<<(2*m+1)表示左括号插头)
if(s[i][j]=='*') //当前位置不能通过
{
if(left==0&&up==0)Insert(state<<2,d[pre][k]);
continue;
}
if(left==0&&up==0)//左边上边都没插头
{
if(i+1>=n||j+1>=m||s[i+1][j]=='*'||s[i][j+1]=='*')continue;
Insert((state<<2)^9,d[pre][k]); //新加入一个连通分量
}
if(left==0&&up) //左边没有插头,上边有
{
if(j+1<m&&s[i][j+1]=='.') //向右延伸插头
{
if(up&(1<<(2*m)))Insert((state<<2)^(up<<2)^1,d[pre][k]); //up为右括号
if(up&(1<<(2*m+1)))Insert((state<<2)^(up<<2)^2,d[pre][k]); //up为左括号
}
if(i+1<n&&s[i+1][j]=='.') //向下延伸插头
{
if(up&(1<<(2*m)))Insert((state<<2)^(up<<2)^4,d[pre][k]); //up为右括号
if(up&(1<<(2*m+1)))Insert((state<<2)^(up<<2)^8,d[pre][k]); //up为左括号
}
}
if(left&&up==0) //上边没有插头,左边有
{
if(j+1<m&&s[i][j+1]=='.') //向右延伸插头
{
if(left&1)Insert((state<<2)^(left<<2)^1,d[pre][k]); //left为右括号
if(left&2)Insert((state<<2)^(left<<2)^2,d[pre][k]); //left为左括号
}
if(i+1<n&&s[i+1][j]=='.') //向下延伸插头
{
if(left&1)Insert((state<<2)^(left<<2)^4,d[pre][k]); //lfet为右括号
if(left&2)Insert((state<<2)^(left<<2)^8,d[pre][k]); //left为左括号
}
}
if(left&&up) //左方上方都有插头
{
if(left==1&&(up&(1<<(2*m+1)))) //left为右括号,up为左括号
{
Insert((state<<2)^(left<<2)^(up<<2),d[pre][k]);
}
if(left==1&&(up&(1<<(2*m)))) //left为右括号,up为右括号
{
for(int sum=1,t=j-1,l=2;t>=0;l+=2,t--)//往左找最近的一个左括号
{
if((2<<l)&state)sum--;
if((1<<l)&state)sum++;
if(sum==0) //找到了,将其改为右括号
{
Insert((state<<2)^(left<<2)^(up<<2)^(3<<(l+2)),d[pre][k]);
break;
}
}
}
if(left==2&&(up&(1<<(2*m)))) //left为左括号,up为右括号
{
if(i==ex&&j==ey) //只有最后一个格子才能合并形成回路
{
ans=d[pre][k];
}
}
if(left==2&&(up&(1<<(2*m+1)))) //left为左括号,up为左括号
{
for(int sum=1,t=j+1,l=2;t<m;l+=2,t++)//往右找最近的一个右括号
{
if(( (1<<(2*m+1)) >>l)&state)sum++;
if(( (1<<(2*m)) >>l)&state)sum--;
if(sum==0) //找到了,将其改为左括号
{
Insert((state<<2)^(left<<2)^(up<<2)^(((1<<(2*m+1))+(1<<(2*m)))>>(l-2)),d[pre][k]);
break;
}
}
}
}
}
memset(d[pre],0,sizeof d[pre]);
total[pre]=0;
now^=1;
pre^=1;
memset(head,-1,sizeof head);
Size=0;
}
}
cout<<ans<<endl;
}
return 0;
}