• ZOJ 3466 The Hive II (插头DP)


    The Hive II

    Time Limit: 5 Seconds      Memory Limit: 65536 KB

    There is a hive in the village. Like this. There are 8 columns(from A to H) in this hive. Different colums have the same number of grids. Every grid has its own coordinate, which is formed by two uppercases, representing the row index and the column index. The row index starts from A. And the hive has less than ten rows in total. The following figure shows a hive with two rows.

    Photo

    There is honey in some grids. A naughty bee discovers this special hive and hopes to eat all honey in the hive. However, this strange bee sets some rules for itself while eating. They are descirbed as following:

    • It must eat the honey by choosing a circuit and then eat all honey that is in the chosen circuit.
    • Honey will disappear immediately after the bee eats it.
    • All grids which are in the circuit should has honey in it.
    • The length of the circuit should be no less than 3.
    • The bee can choose more than one circuit to eat honey.

    Given the hive and the honey in it, how many different ways can this naughty bee eat all honey in the hive?

    Input

    There are multiple test cases.

    For each case, there are two integers N(0 < N ≤ 10) and M(0 ≤ MN * 8) in the first line. N represents the size of the hive, which means there are N rows in the hive in total. All grids have honey in it except for those M grids listed in the following line. Each empty grid is described by its coordinate(using two uppercases).

    Output

    For each case, output the number of different ways the bee can eat all honey in the hive. It's guaranteed that the answer does not exceed 263 - 1.

    Sample Input

    3 5
    BB CD BF AH CG
    

    Sample Output

    3
    

    Hint

    The following figure shows all different ways for the sample. The black grids represent those which are initially empty.

    Photo

    Author: MO, Luyi
    Contest: ZOJ Monthly, January 2011

    这题是插头DP的入门题,把四边形变成了六边形,难度加大了不少。

    但是按照列来写代码简化了不少,就是时间较多。

    被坑了好久,原来ZOJ的long long 一定要用%lld输出的啊。。。。伤不起,对ZOJ不熟悉。

    /*
    ZOJ 3466
    C++ 3850ms 62768K
    原来ZOJ中的long long要用%lld输出啊
    对ZOJ不熟悉啊,被坑了好久
    
    */
    
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int HASH=10007;
    const int STATE=2000010;
    const int MAXD=32;
    int N,M;
    int code[MAXD],maze[MAXD][MAXD];
    
    struct HASHMAP
    {
        int head[HASH],next[STATE],state[STATE],size;
        long long f[STATE];
        void init()
        {
            size=0;
            memset(head,-1,sizeof(head));
        }
        void push(int st,long long ans)
        {
            int i,h=st%HASH;
            for(i=head[h];i!=-1;i=next[i])
              if(st==state[i])
              {
                  f[i]+=ans;
                  return;
              }
            f[size]=ans;
            state[size]=st;
            next[size]=head[h];
            head[h]=size++;
        }
    }hm[2];
    void decode(int *code,int m,int st)
    {
        int i;
        for(i=m;i>=0;i--)
        {
            code[i]=st&1;
            st>>=1;
        }
    }
    int encode(int *code,int m)
    {
        int i,st=0;
        for(i=0;i<=m;i++)
        {
            st<<=1;
            st|=code[i];
        }
        return st;
    }
    void init()
    {
        N=8;//倒过来,8行
        int t;
        scanf("%d",&t);
        memset(maze,0,sizeof(maze));
        for(int i=1;i<=N;i++)
          for(int j=1;j<=M;j++)
            maze[i][j]=1;
        char str[10];
        while(t--)
        {
            scanf("%s",&str);
            maze[str[1]-'A'+1][M-(str[0]-'A')]=0;
        }
    }
    void shift(int *code,int m)//偶数行换奇数行的时候要变化
    {
        for(int i=m;i>1;i--)
          code[i]=code[i-2];
        code[0]=0;
        code[1]=0;
    }
    void dpblank(int i,int j,int cur)
    {
        int k,left,up1,up2;
        int t1,t2;
        if(i%2==0)t1=j,t2=j+1;
        else t1=j-1,t2=j;
        for(k=0;k<hm[cur].size;k++)
        {
            decode(code,2*M,hm[cur].state[k]);
            left=code[2*(j-1)];
            up1=code[2*j-1];
            up2=code[2*j];
           // printf("%d %d:  %d  %d  %d\n",i,j,left,up1,up2);
            if((left==1&&up1==1&&up2==0)||(left==0&&up1==1&&up2==1)||(left==1&&up1==0&&up2==1))
            {
                code[2*j-2]=code[2*j-1]=code[2*j]=0;
                if(j==M&&i%2==0)shift(code,2*M);
                hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
            }
            else if((left==1&&up1==0&&up2==0)||(left==0&&up1==1&&up2==0)||(left==0&&up1==0&&up2==1))
            {
                if(maze[i+1][t1])
                {
                    code[2*j-2]=1;
                    code[2*j-1]=code[2*j]=0;
                    if(j==M&&i%2==0)shift(code,2*M);
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
                if(maze[i+1][t2])
                {
                    code[2*j-2]=code[2*j]=0;
                    code[2*j-1]=1;
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
                if(maze[i][j+1])
                {
                    code[2*j-2]=code[2*j-1]=0;
                    code[2*j]=1;
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
            }
            else if(left==0&&up1==0&&up2==0)
            {
                if(maze[i+1][t1]&&maze[i+1][t2])
                {
                    code[2*j-2]=code[2*j-1]=1;
                    code[2*j]=0;
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
                if(maze[i+1][t1]&&maze[i][j+1])
                {
                    code[2*j-2]=code[2*j]=1;
                    code[2*j-1]=0;
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
                if(maze[i+1][t2]&&maze[i][j+1])
                {
                    code[2*j-2]=0;
                    code[2*j-1]=code[2*j]=1;
                    hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
                }
            }
        }
    }
    void dpblock(int i,int j,int cur)
    {
        int k;
        for(k=0;k<hm[cur].size;k++)
        {
            decode(code,2*M,hm[cur].state[k]);
            code[2*j-2]=code[2*j-1]=code[2*j]=0;
            if(j==M&&i%2==0)shift(code,2*M);
            hm[cur^1].push(encode(code,2*M),hm[cur].f[k]);
        }
    }
    void solve()
    {
        int i,j,cur=0;
        long long ans=0;
        hm[cur].init();
        hm[cur].push(0,1);
        for(i=1;i<=N;i++)
          for(j=1;j<=M;j++)
          {
              hm[cur^1].init();
              if(maze[i][j])dpblank(i,j,cur);
              else dpblock(i,j,cur);
              cur^=1;
          }
        for(i=0;i<hm[cur].size;i++)
          if(hm[cur].state[i]==0)
             ans+=hm[cur].f[i];
        printf("%lld\n",ans);//ZOJ中的C++要lld才能AC的
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(scanf("%d",&M)!=EOF)
        {
            init();
            solve();
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    HDU X mod f(x)(题解注释)
    hdu 3555 Bomb(不要49,数位DP)
    hdu 2089 不要62(入门数位dp)
    暑假练习赛 003 B Chris and Road
    暑假练习赛 003 F Mishka and trip
    暑假练习赛 003 A Spider Man
    linux:关于Linux系统中 CPU Memory IO Network的性能监测
    linux TCP数据包重传过程----小结
    linux TCP头部的构造的简单分析
    linux TCP数据包封装在SKB的过程分析
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2708909.html
Copyright © 2020-2023  润新知