Poj 3278 catch that cow BFS

Source: Internet
Author: User

Source: http://poj.org/problem? Id = 3278

Question: How many steps are required from the start point to the end point for a start point and an end point. You can take one step to the left, one step to the right, or the coordinate is multiplied by 2.

Idea: a raw BFs, a water question.

Code:

 # include 
  
    # include 
   
     # include 
    
      # include 
     
       using namespace STD; # define CLR (ARR, Val) memset (ARR, Val, sizeof (ARR) const int n = 100000; int flag [N]; struct point {int num, CNT;} PP [n + 5]; int BFS (INT begin, int end) {queue 
      
        QQ; CLR (flag, 0 ); QQ. push (BEGIN); flag [begin] = 1; PP [begin]. CNT = 0; while (1) {int x = QQ. front (); QQ. pop (); // printf ("x = % d \ n", x); If (x = END) break; If (X-1> = 0 & X-1 <= N &&! Flag [x-1]) {QQ. push (x-1); flag [x-1] = true; PP [x-1]. CNT = PP [X]. CNT + 1 ;}if (x + 1> = 0 & x + 1 <= N &&! Flag [x + 1]) {QQ. push (x + 1); flag [x + 1] = true; PP [x + 1]. CNT = PP [X]. CNT + 1 ;}if (2 * x> = 0 & 2 * x <= N &&! Flag [2 * x]) {QQ. push (2 * X); flag [2 * x] = true; PP [2 * X]. CNT = PP [X]. CNT + 1 ;}} return PP [end]. CNT;} int main () {int begin, end; while (scanf ("% d", & begin, & End )! = EOF) {int x = BFS (begin, end); printf ("% d \ n", x) ;}return 0 ;}
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.