POJ 1739 Tony's Tour(插头DP)

题目链接:http://poj.org/problem?id=1739

题意:给出一个n*m的含有障碍的格子,从左下角开始走到右下角每个格子只能走一次。有多少种?

思路:改造格子。

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)





#define rush() int C; for(scanf("%d",&C);C--;)

#define Rush(n)  while(scanf("%d",&n)!=-1)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(i64 &x){scanf("%lld",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}



void PR(int x) {printf("%d\n",x);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.4lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}



const int HASHSIZE=100007;

const int N=1000005;



int n,m,code[15],cur,pre,sx,sy;

char s[15][15];

i64 ans;



struct node

{

    int e,next[N],head[HASHSIZE];

    i64 cnt[N],state[N];



    void init()

    {

        clr(head,-1);

        e=0;

    }



    void push(i64 s,i64 val)

    {

        int i,x=s%HASHSIZE;

        for(i=head[x];i!=-1;i=next[i])

        {

            if(state[i]==s)

            {

                cnt[i]+=val;

                return;

            }

        }

        state[e]=s;

        cnt[e]=val;

        next[e]=head[x];

        head[x]=e++;

    }

};





node dp[2];



void decode(int code[],int m,i64 st)

{

    int i;

    FORL0(i,m) code[i]=st&7,st>>=3;

}



i64 encode(int code[],int m)

{

    i64 ans=0;

    int hash[15],i,cnt=1;

    clr(hash,-1);

    hash[0]=0;

    FOR0(i,m+1)

    {

        if(hash[code[i]]==-1) hash[code[i]]=cnt++;

        code[i]=hash[code[i]];

        ans=(ans<<3)|code[i];

    }

    return ans;

}







void update1(int i,int j)

{

    int x,y,k,t,q=j==m?m-1:m;

    i64 c;

    FOR0(k,dp[pre].e)

    {

        decode(code,m,dp[pre].state[k]);

        c=dp[pre].cnt[k];

        x=code[j-1];

        y=code[j];

        if(x&&x==y)

        {

            if(i==sx&&j==sy) ans+=c;

        }

        else if(x&&y&&x!=y)

        {

            code[j-1]=code[j]=0;

            FOR0(t,m+1) if(code[t]==y) code[t]=x;

            dp[cur].push(encode(code,q),c);

        }

        else if(x||y)

        {

            if(i<n&&s[i+1][j])

            {

                code[j-1]=x+y;

                code[j]=0;

                dp[cur].push(encode(code,q),c);

            }

            if(j<m&&s[i][j+1])

            {

                code[j-1]=0;

                code[j]=x+y;

                dp[cur].push(encode(code,q),c);

            }

        }

        else if(x==0&&y==0)

        {

            if(i<n&&j<m&&s[i][j+1]&&s[i+1][j])

            {

                code[j-1]=code[j]=13;

                dp[cur].push(encode(code,q),c);

            }

        }

    }

}



void update2(int i,int j)

{

    int k,q=j==m?m-1:m;

    FOR0(k,dp[pre].e)

    {

        decode(code,m,dp[pre].state[k]);

        if(code[j-1]||code[j]) continue;

        dp[cur].push(encode(code,q),dp[pre].cnt[k]);

    }

}





i64 DP()

{

    ans=0; pre=0,cur=1;

    dp[0].init(); dp[0].push(0,1);

    int i,j;

    FOR1(i,n) FOR1(j,m)

    {

        dp[cur].init();

        if(s[i][j]) update1(i,j);

        else update2(i,j);

        pre^=1;

        cur^=1;

    }

    return ans;

}



int main()

{

    int num=0;

    Rush(n)

    {

        RD(m);

        if(n==0&&m==0) break;

        int i,j;

        FOR1(i,n) RD(s[i+2]+3);

        m+=4; n+=2;

        FOR1(i,m) s[1][i]='.';

        FOR1(i,m) s[2][i]='#';

        s[2][1]=s[2][m]='.';

        FOR(i,3,n) s[i][1]=s[i][m]='.',s[i][2]=s[i][m-1]='#';

        s[n][2]=s[n][m-1]='.';

        FOR1(i,n) FOR1(j,m) s[i][j]=(s[i][j]=='.');

        FOR1(i,n) FOR1(j,m) if(s[i][j])

        {

            sx=i,sy=j;

        }

        PR(DP());

    }

    return 0;

}

  

你可能感兴趣的:(poj)