poj3278 Catch That Cow---bfs

2014-11-24 03:15:30 · 作者: · 浏览: 0

这个 代码混乱。。好歹过了


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           using namespace std; struct node { int cnt; int mm; }tmp; queue
           
             q; int n,vis[100010]; int bfs(int m) { int pp; vis[m]=1; tmp.mm=m; tmp.cnt=1; q.push(tmp); while(!q.empty()) { tmp=q.front(); q.pop(); m=tmp.mm; pp=tmp.cnt; if(m-1==n) return pp; else if(m>=1&&!vis[m-1]){ tmp.mm=m-1; tmp.cnt=pp+1; q.push(tmp); vis[m-1]=1; } if(m+1==n) return pp; else if(m<100000&&!vis[m+1]) { tmp.mm=m+1; tmp.cnt=pp+1; q.push(tmp); vis[m+1]=1; } if(m+m==n) return pp; else if(m+m<100000&&!vis[m+m]){ tmp.mm=m+m; tmp.cnt=pp+1; q.push(tmp); vis[m+m]=1; } } } int main() { int m,i,s,tmp,ans; while(~scanf("%d%d",&m,&n)) { if(m==n) { printf("0\n"); continue; } memset(vis,0,sizeof vis); ans=bfs(m); printf("%d\n",ans); } return 0; }