bzoj1054: [HAOI2008]移動玩具

就是把矩陣當成二進制數判斷是否訪問過,然後就能AC了。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct data
{
    int a[5][5];
    int step;
    int num;
};
queue<data> q;
int read()
{
    char ch=getchar();int f=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
    return f;
}
int vis[100005],pur,s1[5][5],now[5][5];
int movex[5]={0,0,0,1,-1},movey[5]={0,-1,1,0,0};
char s[5][5],t[5][5];
void bfs()
{
    while(!q.empty())
    {
        data x=q.front();
        q.pop();
        //cout<<x.num<<" ";
        for(int i=1;i<=4;i++)
        {
            for(int j=1;j<=4;j++)
            {
                now[i][j]=x.a[i][j];
            }
        }
        for(int i=1;i<=4;i++)
        {
            for(int j=1;j<=4;j++)
            {
                if(now[i][j]==1)
                {
                    for(int k=1;k<=4;k++)
                    {
                        if(!now[i+movex[k]][j+movey[k]]&&i+movex[k]>=1&&i+movex[k]<=4&&j+movey[k]>=1&&j+movey[k]<=4)
                        {
                            now[i+movex[k]][j+movey[k]]=1;
                            now[i][j]=0;
                            int temp=0,now1=1;
                            for(int o=1;o<=4;o++)
                            {
                                for(int p=1;p<=4;p++)
                                {
                                    temp+=now1*now[o][p];
                                    now1<<=1;
                                }
                            }
                            //cout<<temp<<" ";
                            if(vis[temp]>vis[x.num]+1)
                            {
                                vis[temp]=vis[x.num]+1;
                                if(temp==pur) return;
                                data x;
                                x.num=temp;
                                x.step=vis[temp];
                                for(int o=1;o<=4;o++)
                                {
                                    for(int p=1;p<=4;p++)
                                    {
                                        x.a[o][p]=now[o][p];
                                    }
                                }
                                q.push(x);

                            }
                            now[i+movex[k]][j+movey[k]]=0;
                            now[i][j]=1;
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    for(int i=1;i<=4;i++)
    scanf("%s",s[i]+1);
    for(int i=1;i<=4;i++)
    scanf("%s",t[i]+1);
    int now1=1,temp=0;
    memset(vis,0x7f7f7f7f,sizeof(vis));
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            pur+=now1*(t[i][j]-'0');
            temp+=now1*(s[i][j]-'0');
            now1<<=1;
            s1[i][j]=s[i][j]-'0';
        }
    }
    //cout<<pur<<" "<<temp;
    data x;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            x.a[i][j]=s1[i][j];
        }
    }
    x.step=0;
    x.num=temp;
    vis[temp]=0;
    q.push(x);
    bfs();
    cout<<vis[pur];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章