剪枝十分重要 visi数组 记录 已经在树中的值 避免重复
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#define maxn 65536
using namespace std;
char a[5];
int b[5][5];
int aa=0;
int v[maxn],x[maxn],y[maxn],fa[maxn],px[100000],py[100000];
bool visi[maxn];
int bfs()
{
memset(visi,0,sizeof(visi));
for(int i=1;i<=4;++i)
for(int j=1;j<=4;++j)
{
b[i][j]=(1<<((i-1)*4))+(1<<((i-1)*4+1))+(1<<((i-1)*4+2))+(1<<((i-1)*4+3));
for(int m=1;m<=4;++m)
if(m!=i)
b[i][j]+=(1<<((m-1)*4+j-1));
}
int head=0,tail=0,temp,f,xx,yy,t;
v[head]=aa;x[head]=0;y[head]=0;fa[head]=0;
visi[v[head]]=true;
while(true)
{
if(v[head]==0)
return head;
temp=v[head];xx=x[head];yy=y[head];f=head;++head;
for(int i=1;i<=4;++i)
for(int j=1;j<=4;++j)
{
t=temp^b[i][j];
if((i!=xx||j!=yy)&&!visi[t])
{
v[++tail]=t;
x[tail]=i;y[tail]=j;fa[tail]=f;
visi[t]=true;
if(v[tail]==0)
return tail;
}
}
}
}
int main()
{
for(int i=1;i<=4;++i)
{
scanf("%s",a);
for(int j=0;j<4;++j)
if(a[j]=='+')
aa+=1<<((i-1)*4+j);
}
int t=bfs();
int ans=0;
while(t!=0)
{
px[ans]=x[t];
py[ans]=y[t];
++ans;
t=fa[t];
}
printf("%d\n",ans);
for(int i=ans-1;i>=0;--i)
{
printf("%d %d\n",px[i],py[i]);
}
return 0;
}