hdu 4285 circuits

circuits

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 592    Accepted Submission(s): 175


Problem Description
  Given a map of N * M (2 <= N, M <= 12) , '.' means empty, '*' means walls. You need to build K circuits and no circuits could be nested in another. A circuit is a route connecting adjacent cells in a cell sequence, and also connect the first cell and the last cell. Each cell should be exactly in one circuit. How many ways do we have?

 

Input
  The first line of input has an integer T, number of cases.
  For each case:
  The first line has three integers N M K, as described above.
  Then the following N lines each has M characters, ‘.’ or ‘*’.
 

Output
  For each case output one lines.
  Each line is the answer % 1000000007 to the case.
 

Sample Input
  
  
2 4 4 1 **.. .... .... .... 4 4 1 .... .... .... ....
 

Sample Output
  
  
2 6
 

Source
 

Recommend
liuyiding
   这题和hdu 1693 Eat the Trees类似只是限制的回路的个数。所以在hashmap里再加一个记录回路数的数组就行了。
对于判断是否有嵌套的情况就判断当前格之前有多少个插头。若是有奇数个。说明肯定有嵌套。画画图就明白了。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

const int HASH=30007;//哈希表的大小
const int STATE=1000010;//状态数
const int MAXD=15;
const int MOD=1000000007;
int N,M,mac;//mac目标回路数
int code[MAXD],maze[MAXD][MAXD],mcod[MAXD];//编码。和存地图。最小表示法
char s[20];

struct HASHMAP//哈希表结构
{
    int head[HASH],next[STATE],sz;//哈希表头指针模数相同的状态用链表连接。方便状态查找和判重
    long long f[STATE],state[STATE];//f记录对应状态的方法数。next指向模数相同的下一个状态。state记录状态。sz记录状态总数
    int circle[STATE];
    void init()//哈希表初始化函数
    {
        sz=0;
        memset(head,-1,sizeof(head));
    }
    void push(long long st,long long ans,int cir)//压入状态和方法数
    {
        int i,h=st%HASH;
        for(i=head[h]; i!=-1; i=next[i])
            if(st==state[i]&&cir==circle[i])//若状态已经存在。方法数增加就行
            {                               //注意要回路数也相同才能算等效
                f[i]+=ans;
                f[i]%=MOD;
                return;
            }
        f[sz]=ans;//存入新的可行状态
        state[sz]=st;
        circle[sz]=cir;
        next[sz]=head[h];
        head[h]=sz++;
    }
} hm[2];

void decode(int *code,int m,long long st)//编码从高位到低位m到0编码。对应从左到右的插头
{
    int i;
    for(i=m; i>=0; i--)
    {
        code[i]=st&7;
        st>>=3;
    }
}
long long encode(int *code,int m)//最小表示法解码到st中
{
    int i,cnt=1;
    long long st=0;
    memset(mcod,-1,sizeof mcod);
    mcod[0]=0;
    for( i=0; i<=m; i++)
    {
        if(mcod[code[i]]==-1)
            mcod[code[i]]=cnt++;
        code[i]=mcod[code[i]];
        st<<=3;
        st|=code[i];
    }
    return st;
}
void init()//读数据。初始化
{
    int i,j;
    memset(maze,0,sizeof maze);
    for(i=1; i<=N; i++)
    {
        scanf("%s",s+1);
        for( j=1; j<=M; j++)
            if(s[j]=='.')
                maze[i][j]=1;
    }
}

void dpblank(int i,int j,int cur)//处理可到格的情况
{
    int k,t,left,up,temp,cot;
    for(k=0; k<hm[cur].sz; k++) //遍历j格出轮廓线的状态进行状态转移
    {
        if(hm[cur].circle[k]>mac)
            continue;
        decode(code,M,hm[cur].state[k]);//对状态进行编码
        left=code[j-1];//获取左插头状态
        up=code[j];//获取上插头状态
        if(left&&up)//11  -> 00
        {
            if(left==up)//形成回路
            {
                cot=0;
                for(t=0;t<j-1;t++)//判断在当前格之前有多少插头
                    if(code[t])
                    cot++;
               if(cot&1)//如果插头数是奇数说明有嵌套的情况。自己画画就明白了
                continue;
                code[j-1]=code[j]=0;//只能为0一个格子只能两个插头
                hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k],hm[cur].circle[k]+1);//压入新状态
            }
            else
            {
                code[j-1]=code[j]=0;
                for(t=0; t<=M; t++)
                    if(code[t]==up)
                    {
                        code[t]=left;
                        break;
                    }
                hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k],hm[cur].circle[k]);//压入新状态
            }
        }
        else if((!left&&up)||(left&&!up))//01 或 10
        {
            if(up)
                temp=up;
            else
                temp=left;
            if(maze[i][j+1])//j==m时maze[i][j]为0也不用shift
            {
                code[j-1]=0;
                code[j]=temp;
                hm[cur^1].push(encode(code,M),hm[cur].f[k],hm[cur].circle[k]);
            }
            if(maze[i+1][j])
            {
                code[j-1]=temp;
                code[j]=0;
                hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k],hm[cur].circle[k]);
            }
        }
        else
        {
            if(maze[i][j+1]&&maze[i+1][j])//若j==m不会合法所以不用shift
            {
                code[j]=code[j-1]=13;
                hm[cur^1].push(encode(code,M),hm[cur].f[k],hm[cur].circle[k]);
            }
        }
    }
}

void dpblock(int i,int j,int cur)//处理不能到格的情况
{
    int k;
    for(k=0; k<hm[cur].sz; k++) //存入状态均合法不用判断
    {
        decode(code,M,hm[cur].state[k]);//先编码
        code[j-1]=code[j]=0;//肯定不能用插头
        hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k],hm[cur].circle[k]);
    }
}

void solve()
{
    int i,j,cur=0;
    long long ans=0;
    hm[cur].init();//cur用于滚动数组。节约空间。cur存当前格上插头和左插头情况。cur^1用于记录转移出的新状态即下一格
    hm[cur].push(0,1,0);
    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].sz; i++)
        if(hm[cur].circle[i]==mac)
        ans+=hm[cur].f[i];
    printf("%I64d\n",ans);
}
int main()
{
    int cas;

    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d%d",&N,&M,&mac);
        init();
        solve();
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值