码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3278

时间:2014-08-10 10:13:50      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   art   ar   

bubuko.com,布布扣
#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;     
                                  }


 

hdu 3278,布布扣,bubuko.com

hdu 3278

标签:style   blog   http   color   os   io   art   ar   

原文地址:http://www.cnblogs.com/2014acm/p/3902349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!