hdu 4090 bfs+dfs+状态压缩+hash判重

这是11年北京赛区的J题,最近拿来做的时候看这题第一反应就是bfs+dfs..

结果交上去直接MLE了,自己测了个8*8的数据居然花了2G内存%>_<%

后来仔细想想状态可以压缩起来存(因为颜色只有6种,一行最多8个,状压就是7^8,一个int就可以保存,这样只用一个长度为8的数组就可以了。

然后因为会有很多重复的状态,用hash来判重。

这样写完还出现了些小bug,debug后AC,发现居然拿了第一


然后看了别人的解法都跟我不同....我的代码长度略长了点...不过有这速度还是值得的


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=8;
int N,M,K;
const int dx[]= {-1,0,1,-1,1,-1,0,1};
const int dy[]= {1,1,1,0,0,-1,-1,-1};
const int MOD=999997;


struct State
{
    int map[maxn][maxn];
    void print()
    {
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<M; j++)
                printf("%d ",map[i][j]);
            puts("");
        }
    }
    void fall()
    {
        int k;
        for(int i=0; i<M; i++)
        {
            for(int j=N-1; j>=0; j--)
            {
                if(map[j][i])
                {
                    k=j+1;
                    while(!map[k][i]&&k<N)
                        k++;
                    if(k!=j+1)
                    {
                        map[k-1][i]=map[j][i];
                        map[j][i]=0;
                    }
                }
            }
        }
        for(int s=0; s<M-1; s++)
        {
            bool flag=0;
            for(k=s; k<M; k++)
            {
                for(int j=0; j<N; j++)
                {
                    if(map[j][k])
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag) break;
            }
            if(flag&&k>s)
            {
                for(int i=s;i+k-s<M; i++)
                {
                    for(int j=0; j<N; j++)
                    {
                        map[j][i]=map[j][i+k-s];
                        map[j][i+k-s]=0;
                    }
                }
            }
        }
    }
};
State startst;

struct zipState
{
    int map[maxn];
    bool operator==(const zipState &a)
    {
        for(int i=0; i<N; i++)
            if(map[i]!=a.map[i])
                return false;
        return true;
    }
    zipState() {}
    zipState(const State &a)
    {
        getZiped(a);
    }
    void getZiped(const State &a)
    {
        for(int i=0; i<N; i++)
        {
            map[i]=0;
            for(int j=0; j<M; j++)
                map[i]=map[i]*(K+1)+a.map[i][j];
        }
    }
    State unZip()
    {
        State res;
        int now;
        for(int i=0; i<N; i++)
        {
            now=map[i];
            for(int j=M-1; j>=0; j--)
            {
                res.map[i][j]=now%(K+1);
                now/=(K+1);
            }
        }
        return res;
    }
    int getHas()
    {
        int res=0;
        for(int i=0; i<N; i++)
            res+=map[i];
        return res%MOD;
    }
};

struct HasNode
{
    zipState zp;
    int sc;
    int next;
};

struct Has
{
    HasNode has[1000000];
    int box[MOD];
    int end;
    bool insert(zipState zp,int sc)
    {
        int h=zp.getHas();
        for(int i=box[h]; i!=-1; i=has[i].next)
        {
            if(zp==has[i].zp)
            {
                if(sc>has[i].sc)
                {
                    has[i].sc=sc;
                    return true;
                }
                else return false;
            }
        }
        has[end].zp=zp;
        has[end].sc=sc;
        has[end].next=box[h];
        box[h]=end++;
        return true;
    }
    void clear()
    {
        memset(box,-1,sizeof(box));
        end=0;
    }
};
Has has;
bool vis[maxn][maxn];

int dfs(State& st,int x,int y,int col)
{
    st.map[x][y]=0;
    vis[x][y]=1;
    int nx,ny;
    int res=1;
    for(int i=0; i<8; i++)
    {
        nx=x+dx[i];
        ny=y+dy[i];
        if(nx<0||nx>=N||ny<0||ny>=M) continue;
        if(st.map[nx][ny]==col)
        {
            res+=dfs(st,nx,ny,col);
        }
    }
    return res;
}


struct FSO
{
    zipState st;
    int score;
    FSO(zipState ss,int s):st(ss),score(s) {}
    FSO() {}
};

queue<FSO> que;

int bfs()
{
  //  startst.fall();
    que.push(FSO(zipState(startst),0));
    FSO now;
    State nowst;
    State nst;
    zipState nzst;
    int sc;
    int res=0;
    while(!que.empty())
    {
        memset(vis,0,sizeof(vis));
        now=que.front();
        res=max(res,now.score);
        que.pop();
        nowst=now.st.unZip();
        for(int i=0; i<N; i++)
            for(int j=0; j<M; j++)
            {
                if(!vis[i][j]&&nowst.map[i][j])
                {
                    nst=nowst;
                    sc=dfs(nst,i,j,nst.map[i][j]);
                    if(sc>=3)
                    {
                        /*
                        if(N)
                        {
                            nowst.print();
                            puts("");
                            printf("%d %d %d\n",i,j,nowst.map[i][j]);
                            printf("%d\n",now.score+sc*sc);
                            puts("");
                            nst.print();
                            puts("");
                            nst.fall();
                            nst.print();
                            puts("");
                            puts("------");
                        }
                        else*/
                        nst.fall();
                        nzst.getZiped(nst);
                        if(has.insert(nzst,now.score+sc*sc))
                            que.push(FSO(nst,now.score+sc*sc));
                    }
                }
            }
    }
    return res;
}

void work()
{
    printf("%d\n",bfs());
}

void init()
{
    has.clear();
}

void input()
{
    for(int i=0; i<N; i++)
        for(int j=0; j<M; j++)
            scanf("%d",&startst.map[i][j]);
}


int main()
{
  //  freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    while(~scanf("%d%d%d",&N,&M,&K))
    {
        init();
        input();
        work();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值