http://www.cnblogs.com/yangcl/archive/2012/01/14/2322433.html
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,ans;
int vis[1<<20+1],dis[1<<20+1];
int bfs(int x)
{
if(0==x)
return 0;
int i,temp;
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
q.push(x);
vis[x]=1;
dis[0]=0;
while(!q.empty())
{
x=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(i==1)
temp=x^3;
else
temp=x^(7<<i-2);
temp&=(1<<n)-1;
if(vis[temp])
continue;
vis[temp]=1;
dis[temp]=dis[x]+1;
if(temp==0)
return dis[temp];
q.push(temp);
}
}
return -1;
}
int main()
{
char str[100];
int a;
int i;
while(cin>>str)
{
a=0;
n=strlen(str);
for(i=0;i<n;i++)
a=(a*2)+str[i]-'0';
ans=bfs(a);
if(ans<0)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}