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;     
                                      }


     

  • 相关阅读:
    NodeJS学习笔记之Connect中间件模块(一)
    XML CDATA节点
    6个强大的AngularJS扩展应用
    java使用poi读取ppt文件和poi读取excel、word示例
    Java log4j详细教程
    JAVA8 十大新特性详解
    Express 路由
    基于 Node.js 平台,快速、开放、极简的 web 开发框架。
    Node.js 手册查询-1-核心模块方法
    Node.js 手册查询-2-MongoDB数据库方法
  • 原文地址:https://www.cnblogs.com/2014acm/p/3902349.html
Copyright © 2011-2022 走看看