zoukankan      html  css  js  c++  java
  • hdu 3278

    #include <iostream>
    #include <queue>
    #include <stdio.h>
    #include <string>
    using namespace std;
    int a,b,used[200020];
    queue<int>q;
    int ok(int n)
    { 
           if(n < 200000&&n >= 0&&used[n]==0)  return 1; 
                  return 0;
                  }
                  
     void bfs()
    {        int n;  
          while(!q.empty()) 
                 { 
                        //      cout<<q.front()<<endl;  
                                      if(q.front() == b) return ; 
                                                     n = q.front() - 1; 
        if(ok(n)){used[n] = used[q.front()]+1;q.push(n);}     
                   n = q.front() + 1;  
      if(ok(n)){used[n] = used[q.front()]+1;q.push(n);
      }                n = q.front()*2; 
                     if(ok(n)){used[n] = used[q.front()]+1;q.push(n);
                     }                q.pop();        
                     }
                     }
                     
                     int main()
                     {     
                        while(cin>>a>>b)   
                             {             
                                memset(used,0,sizeof(used));          
                                      q.push(a);                used[a] = 1;       
                                               bfs();           
                                                    cout<<used[b]-1<<endl;        
                                                            while(!q.empty())       
                                                                     {              
                                                                               q.pop();      
                                                                                         }  
                                                                                           
                                                                                               }  
                                                                                                     return 0;      
                                                                                                              }
    View Code

    #include <iostream>
    #include <queue>
    #include <stdio.h>
    #include <string>
    using namespace std;
    int a,b,used[200020];
    queue<int>q;
    int ok(int n)
    {
           if(n < 200000&&n >= 0&&used[n]==0)  return 1;
               return 0;
         }
        
     void bfs()
    {        int n; 
          while(!q.empty())
              {
               //      cout<<q.front()<<endl; 
                       if(q.front() == b) return ;
                             n = q.front() - 1;
     if(ok(n)){used[n] = used[q.front()]+1;q.push(n);}    
                n = q.front() + 1; 
      if(ok(n)){used[n] = used[q.front()]+1;q.push(n);
      }                n = q.front()*2;
                     if(ok(n)){used[n] = used[q.front()]+1;q.push(n);
         }                q.pop();       
         }
         }
        
         int main()
         {    
            while(cin>>a>>b)  
              {            
              memset(used,0,sizeof(used));         
                 q.push(a);                used[a] = 1;      
                       bfs();          
                      cout<<used[b]-1<<endl;       
                        while(!q.empty())      
                           {             
                               q.pop();     
                                   } 
                           
                                } 
                                   return 0;     
                                      }


     

  • 相关阅读:
    07-0.部署 worker 节点
    06-4.部署高可用 kube-scheduler 集群
    06-3.部署高可用 kube-controller-manager 集群
    vim 查找并替换多个匹配字符
    vim 行号的显示与隐藏
    Python学习【第4篇】:元组魔法
    Python学习【第3篇】:列表魔法
    Python学习【第2篇】:基本数据类型(详解)
    pycharm设置头文件模板(for mac)
    pycharm创建文件夹以及查看源文件存放位置(FOR MAC)
  • 原文地址:https://www.cnblogs.com/2014acm/p/3902349.html
Copyright © 2011-2022 走看看