[bzoj1054][HAOI2008]移动玩具

http://www.lydsy.com/JudgeOnline/problem.php?id=1054

无聊时想练一下构图又懒得打广搜……
发现状态只有2^16种
于是根据相邻状态连边
(最多只有2^22条边(极有可能不到))
然后直接跑最短路即可

代码如下

#include <bits/stdc++.h>
using namespace std;
#define N 100005
#define M 4200000
#define inf 1000000000
char s[10];
int num,to[M],head[N],Next[M];
int dis[N],flag[N],a[4][4],b[4][4];
int n;
struct node
{
    int x,y;
};
bool operator > (node x,node y)
{
    return x.y<y.y;
}
bool operator < (node x,node y)
{
    return x.y>y.y;
}
priority_queue <node> heap;
inline bool check1(int x,int y)
{
    if (x>2)
        if (a[x-2][y]^a[x-1][y]) return 0;
    if (y)
        if (a[x-1][y-1]^a[x-1][y]) return 0;
    if (y<3)
        if (a[x-1][y+1]^a[x-1][y]) return 0;
    return 1;
}
inline bool check2(int x,int y)
{
    if (x>2)
        if (a[x-2][y]^a[x-1][y]) return 0;
    if (y)
        if (a[x-1][y-1]^a[x-1][y]) return 0;
    return 1;
}
inline void add(int st,int x,int y,int xx,int yy)
{
    swap(b[x][y],b[xx][yy]);
    int t=0;
    for (int i=3;i>=0;--i)
        for (int j=3;j>=0;--j)
            t=(t<<1)+(b[i][j]&1);
    ++num;
    to[num]=t;
    Next[num]=head[st];
    head[st]=num;
    swap(b[x][y],b[xx][yy]);
}
int main()
{
    n=1<<16;
    for (int i=0;i<(1<<16);++i)
    {
        int t=i;
        for (int i1=0;i1<4;++i1)
            for (int j1=0;j1<4;++j1)
                a[i1][j1]=b[i1][j1]=t&1,t>>=1;
        for (int i1=0;i1<4;++i1)
            for (int j1=0;j1<4;++j1)
            {
                if (i1) if (a[i1][j1]^a[i1-1][j1]) if (check1(i1,j1)) add(i,i1,j1,i1-1,j1);
                if (j1) if (a[i1][j1]^a[i1][j1-1]) if (check2(i1,j1)) add(i,i1,j1,i1,j1-1);
                if (i1<3) if (a[i1][j1]^a[i1+1][j1]) add(i,i1,j1,i1+1,j1);
                if (j1<3) if (a[i1][j1]^a[i1][j1+1]) add(i,i1,j1,i1,j1+1);
            }
    }
    int st=0,ed=0;
    for (int i=0;i<4;++i)
    {
        scanf("%s",s);
        for (int j=0;j<4;++j) a[i][j]=s[j]-'0';
    }
    for (int i=3;i>=0;--i)
        for (int j=3;j>=0;--j)
            st=(st<<1)+(a[i][j]&1);
    for (int i=0;i<4;++i)
    {
        scanf("%s",s);
        for (int j=0;j<4;++j) a[i][j]=s[j]-'0';
    }
    for (int i=3;i>=0;--i)
        for (int j=3;j>=0;--j)
            ed=(ed<<1)+(a[i][j]&1);
    //cout<<"st="<<st<<endl;cout<<"ed="<<ed<<endl;
    for (int i=0;i<n;++i) dis[i]=inf,flag[i]=1;
    dis[st]=0;
    heap.push({st,0});
    while (!heap.empty())
    {
        int t;
        do
        {
            t=heap.top().x;
            heap.pop();
        }while (!flag[t]);
        flag[t]=0;
        for (int j=head[t];j;j=Next[j])
        if (flag[to[j]] && dis[to[j]]>dis[t]+1)
        {
            dis[to[j]]=dis[t]+1;
            heap.push({to[j],dis[to[j]]});
        }
    }
    cout<<dis[ed]<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值