您的位置:首页 > 其它

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

2011-06-08 19:51 330 查看
Tony's Tour

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 1680Accepted: 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
LouTiancheng@POJ

分析:传说中的男人八题,哈哈,此题一看以为是插头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
[0]=g
[m-1]=1;
        printf("%lld\n",PlugDP());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: