您的位置:首页 > 其它

bzoj1054: [HAOI2008]移动玩具

2017-10-23 09:19 253 查看
就是把矩阵当成二进制数判断是否访问过,然后就能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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: