【bzoj1054】[HAOI2008]移動玩具

來源:互聯網
上載者:User

標籤:

#include<algorithm>#include<iostream> #include<cstring>#include<cstdlib> #include<cstdio>#include<cmath> #include<queue>  using namespace std;  int xx[5]={0,0,1,-1},    yy[5]={1,-1,0,0}; int bg,ed,h,x,y;int t,w=1; bool ans[5][5],mark[100010]; char ch[5]; struct data{    bool a[5][5];    int step;}q[100010]; int hash(bool a[5][5]){    int k=1,s=0;    for (int i=1;i<=4;i++)        for (int j=1;j<=4;j++)        {            s+=a[i][j]*k;            k<<=1;        }    return s;} int main(){    for (int i=1;i<=4;i++)    {        scanf("%s",ch);        for (int j=1;j<=4;j++)            q[0].a[i][j]=ch[j-1]-‘0‘;    }    for (int i=1;i<=4;i++)    {        scanf("%s",ch);        for (int j=1;j<=4;j++)            ans[i][j]=ch[j-1]-‘0‘;    }    bg=hash(q[t].a);    ed=hash(ans);    if (bg==ed)    {        printf("0");        return 0;    }    mark[bg]=1;    while (t<w)    {        for (int i=1;i<=4;i++)            for (int j=1;j<=4;j++)                if (q[t].a[i][j])                    for (int k=0;k<4;k++)                    {                        x=xx[k]+i;                        y=yy[k]+j;                        if (q[t].a[x][y] || x>4 || y>4 ||x<1 || y<1)                            continue;                        swap(q[t].a[i][j],q[t].a[x][y]);                        h=hash(q[t].a);                        if (!mark[h])                        {                            if (h==ed)                            {                                printf("%d",q[t].step+1);                                return 0;                            }                            mark[h]=1;                            memcpy(q[w].a,q[t].a,sizeof(q[w].a));                            q[w].step=q[t].step+1;                            w++;                        }                        swap(q[t].a[i][j],q[t].a[x][y]);                    }        t++;    }    return 0;}

  

bfs

【bzoj1054】[HAOI2008]移動玩具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.