Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=2209

BFS

我的代码
 1 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int maxa=1100000;
6 char s[25];
7 int a,vis[maxa],dis[maxa],l;
8 queue<int> q;
9 int bfs(int x)
10 {
11 if (x==0) return 0;
12 while (!q.empty()) q.pop();
13 q.push(x); vis[x]=1; dis[x]=0;
14 while (!q.empty())
15 {
16 x=q.front(); q.pop();
17 for (int i=1;i<=l;i++)
18 {
19 int nx;
20 if (i==1) nx=x^3;
21 else nx=x^(7<<i-2);
22 nx&=(1<<l)-1;
23 if (vis[nx]) continue;
24 vis[nx]=1; dis[nx]=dis[x]+1;
25 if (nx==0) return dis[nx];
26 q.push(nx);
27 }
28 }
29 return -1;
30 }
31 int main()
32 {
33 int i,ans;
34 while (~scanf("%s",s))
35 {
36 a=0;
37 memset(vis,0,sizeof(vis));
38 memset(dis,0,sizeof(dis));
39 l=strlen(s);
40 for (i=0;s[i];i++) a=a*2+s[i]-'0';
41 ans=bfs(a);
42 if (ans<0) printf("NO\n");
43 else printf("%d\n",ans);
44 }
45 return 0;
46 }

 

posted on 2012-01-14 16:02  Qiuqiqiu  阅读(890)  评论(0编辑  收藏  举报