题目链接:
题目大意:
给你开始和结束两张4x4的01图,每次操作只能够交换相邻的两个格子(有公共边),问最少的操作步数。
题目思路:
【搜索】
这题一看就是一道BFS(宽搜),判重的时候2进制压位就好,总共16位,最大不会超过216 -1(65535)。
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) (a)*(a)
#define swap(a,b) (a)^=(b),(b)^=(a),(a)^=(b)
#define eps 1e-8
#define S 10000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 24
#define M 65544
using namespace std;
int n,m,cas,lll,ans;
int r[N]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
char s1[N];
int map[4][4];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int s,e;
int q[M],v[M];
bool u[M];
int work()
{
int i,j,k,h,t,now,xx,yy,x;
q[1]=e;h=0;t=1;
while(h++<t)
{
now=q[h];
if(now==s)return v[h];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
xx=i+dx[k];
yy=j+dy[k];
if(0<=xx && xx<4 && 0<=yy && yy<4)
{
map[i][j]=(now&r[15-i*4-j])>0;map[xx][yy]=(now&r[15-4*xx-yy])>0;
x=now-map[i][j]*r[15-i*4-j]+map[i][j]*r[15-4*xx-yy]+map[xx][yy]*r[15-4*i-j]-map[xx][yy]*r[15-xx*4-yy];
if(!u[x])
{
u[x]=1;
q[++t]=x;
v[t]=v[h]+1;
}
}
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
// while(~scanf("%d",&n) && n)
// {
for(i=0,j=15;i<4;i++)
{
scanf("%s",s1);
for(k=0;k<4;k++,j--)
s+=(s1[k]-'0')*r[j];
}
for(i=0,j=15;i<4;i++)
{
scanf("%s",s1);
for(k=0;k<4;k++,j--)
{
e+=(s1[k]-'0')*r[j];
map[i][k]=s1[k]-'0';
}
}
printf("%d\n",work());
// }
return 0;
}
/*
//
//
*/