POJ 1753
枚举,压缩状态,bfs~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
int pow2[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
int change[16]={51200,58368,29184,12544,
35968,20032,10016,4880,
2248,1252,626,305,
140,78,39,19};
int tar1,tar2;
int hash[131080];
struct node
{
int dep;
int n;
};
queue<node> q;
int main()
{
int ori,ite,i,t;
char tc;
node temp,tn;
ite=15;
ori=0;
tar1=0;
tar2=65535;
memset(hash,0,sizeof(hash));
while (scanf("%c",&tc) != EOF)
{
if (tc == 'b')
{
ori+=pow2[ite];
ite--;
}
else if (tc == 'w')
{
ite--;
}
}
hash[ori]=1;
temp.dep=0;
temp.n=ori;
if (ori == tar1 || ori == tar2)
{
printf("0\n");
return 0;
}
while (!q.empty())
q.pop();
q.push(temp);
while (!q.empty())
{
temp=q.front();
q.pop();
for (i=0; i<16; i++)
{
t=temp.n^change[i];
if (t == tar1 || t == tar2)
{
printf("%d\n",temp.dep+1);
return 0;
}
if (hash[t] == 1)
continue;
tn.n=t;
hash[t]=1;
tn.dep=temp.dep+1;
q.push(tn);
}
}
printf("Impossible\n");
return 0;
}