poj 1739(男人八题之插头DP)

16 篇文章 0 订阅

 

Tony's Tour
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1680 Accepted: 751

Description

A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked. The Farm is located in the lower left plot and the Market is located in the lower right plot. Tony takes her tour of the township going from Farm to Market by walking through every unblocked plot exactly once. 
Write a program that will count how many unique tours Betsy can take in going from Farm to Market. 

Input

The input contains several test cases. The first line of each test case contain two integer numbers n,m, denoting the number of rows and columns of the farm. The following n lines each contains m characters, describe the farm. A '#' means a blocked square, a '.' means a unblocked square. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

Sample Input

2 2
..
..
2 3
#..
...
3 4
....
....
....
0 0

Sample Output

1
1
4

Source

分析:传说中的男人八题,哈哈,此题一看以为是插头DP简单路径问题,但是仔细想,由于起点和终点固定,所以完全可以用一条回路的算法,具体做法和ural 1519类似,直接套用那题的模板,稍微改一下结尾,哈哈。。。水过

代码:

#include<cstdio>
#include<cstring>
#define mm 1007
#define mn 11
struct hashtable
{
    int h[mm],s[mm],p[mm],sz;
    long long d[mm];
    int hash(int x,long long v)
    {
        int c=x%mm,i;
        for(i=h[c];i>=0;i=p[i])
        if(s[i]==x)
        {
            d[i]+=v;
            return i;
        }
        d[sz]=v,s[sz]=x,p[sz]=h[c],h[c]=sz;
        return sz++;
    }
    void clear()
    {
        sz=0,memset(h,-1,sizeof(h));
    }
}f[2];
bool g[15][15];
int g1,g2,i,j,k,x,y,s,n,m;
int eat(bool f)
{
    int a=x<<(j<<1),b=(3^x)<<((j+f)<<1),c=3<<((j+f)<<1),n=1;
    s=s^a^(a<<2);
    if(f)a<<=2;
    while(n)
    {
        if(f)a<<=2,b<<=2,c<<=2;
        else a>>=2,b>>=2,c>>=2;
        x=c&s;
        if(x==a)++n;
        if(x==b)--n;
    }
    return (s^b)|a;
}
inline bool ok(int c)
{
    if(c==1)return g[i+1][j];
    if(c==2)return g[i][j+1];
    if(c==3)return g[i+1][j]&&g[i][j+1];
    return 1;
}
void move(long long v)
{
    if(x==0&&y==0)
    {
        if(ok(3))f[g2].hash(s|9<<(j<<1),v);
    }
    else if(x==0||y==0)
    {
        if(x)y=1;else x=y,y=2;
        x=x<<(j<<1);
        if(ok(y))f[g2].hash(s,v);
        if(ok(3^y))f[g2].hash(s^x^(x<<2),v);
    }
    else if(x==y)f[g2].hash(eat(x==1),v);
    else if(x==2&&y==1)f[g2].hash(s^(6<<(j<<1)),v);
}
long long PlugDP()
{
    f[0].clear();
    f[0].hash(0,1);
    for(g1=1,g2=i=0;i<n;++i)
    {
        for(j=0;j<f[g2].sz;++j)f[g2].s[j]<<=2;
        for(j=0;j<m;++j)
            if(g[i][j])for(g1=!g1,g2=!g2,f[g2].clear(),k=0;k<f[g1].sz;++k)
            {
                s=f[g1].s[k],x=(s>>(j<<1))&3,y=(s>>((j+1)<<1))&3;
                move(f[g1].d[k]);
            }
    }
    long long ret=0;
    for(i=0;i<f[g2].sz;++i)
        if(f[g2].s[i]==(1|(2<<((m-1)<<1))))ret+=f[g2].d[i];
    return ret;
}
int main()
{
    char c;
    while(scanf("%d%d",&n,&m),n)
    {
        memset(g,0,sizeof(g));
        for(i=0;i<n;++i)
            for(j=0;j<m;++j)
                scanf(" %c",&c),g[i][j]=(c=='.');
        g[n][0]=g[n][m-1]=1;
        printf("%lld\n",PlugDP());
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值