【题解】vijos P1206 coVH之再破难关

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int mapp[5][5];
struct node
{
int map[5][5];
int num;
int step;
}team[67000];
int a[17];
int b[17];
bool pd[67000];
int ans=0,ans1=0;
int w=1;
int change(int map[5][5])
{
int ans=0,w=1;
for(int i=4;i>=1;i--)
{
for(int j=4;j>=1;j--)
{
ans+=w*map[i][j];
w*=2;
}
}
return ans;
}
void bfs()
{
int s=1,t=1;
t++;
pd[ans1]=true;

while(s!=t)
{

node now=team[s];
s++;
//	cout<<now.num<<' '<<now.step<<endl;
if(now.num==ans)
{
printf("%d",now.step);
exit(0);
}

for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i-1>=1&&now.map[i][j]!=now.map[i-1][j])
{
swap(now.map[i][j],now.map[i-1][j]);
now.step++;
int xx=change(now.map);
if(pd[xx]!=true)
{
team[t]=now;
team[t].num=xx;
t++;
pd[xx]=true;
}
swap(now.map[i][j],now.map[i-1][j]);
now.step--;
}
if(j-1>=1&&now.map[i][j]!=now.map[i][j-1])
{
swap(now.map[i][j],now.map[i][j-1]);
now.step++;
int xx=change(now.map);
if(pd[xx]!=true)
{
team[t]=now;
team[t].num=xx;
t++;
pd[xx]=true;
}
swap(now.map[i][j],now.map[i][j-1]);
now.step--;
}
}
}

}
return ;
}
int main()
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
char ch;
cin>>ch;
team[1].map[i][j]=ch-'0';
}
}
int tot=0;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
char ch;
cin>>ch;
a[++tot]=ch-'0';
}
}

for(int i=tot;i>=1;i--)
{
ans+=w*a[i];
w*=2;
}
team[1].num=change(team[1].map);
team[1].step=0;
bfs();
return 0;
}