Acdream 1025 BFS

Source: Internet
Author: User

TransformTime limit:4000/2000ms (java/others) Memory limit:128000/64000kb (java/others) Submit statistic Next Problemproblem Description

One can transformxIntox+d IfDis divisor ofx. Find out the minimum number of steps to transform a into b.

Input

Integers a and b.

(1≤a,b≤5)

Output

The only integer denotes the minimum number of steps. Print −1if impossible.

Sample Input
1 6
Sample Output
3
Hint1 →2 →4 →6 or 1 & #x2192; 2 & #x2192; 3 & #x2192; 6 " > 1→2 →3 →6 Sourceftiaschmanagernanae Test instructions: The factor of x changing to X+d D is the number of steps that change from a to B at least the output-1: The processing of the BFS attention factor is handled if (GG.X<=B&&VI S[gg.x]==0) Note Two comparison of sequential vis array re multiple
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <stack>6#include <cmath>7 #definell Long Long8 #definePi ACOs (-1.0)9 #defineMoD 1000000007Ten using namespacestd; One intb; A structnode - { -     intx; the     intStep; - }exm,gg; -Queue<node>Q; - intvis[100005]; + voidBFsintSt) - { +memset (Vis,0,sizeof(Vis)); A      while(!q.empty ()) at Q.pop (); -exm.x=St; -exm.step=0; -vis[st]=1; - Q.push (EXM); -     while(!q.empty ()) in    { -exm=Q.front (); to Q.pop (); +        if(exm.x==b) -        { thecout<<exm.step<<Endl; *             Break; $     }Panax Notoginseng         for(intI=1; I<=sqrt (exm.x); i++) -        { the            if(exm.x%i==0) +            { Agg.x=exm.x+i; thegg.step=exm.step+1; +                if(gg.x<=b&&vis[gg.x]==0) -                { $ Q.push (GG); $vis[gg.x]=1; -             } -gg.x=exm.x+exm.x/i; thegg.step=exm.step+1; -                if(gg.x<=b&&vis[gg.x]==0)Wuyi                { the Q.push (GG); -vis[gg.x]=1; Wu             } -         } About     } $    }   - } - intMain () - { A      while(SCANF ("%d%d", &a,&b)! =EOF) +     { the         if(a<=b) - BFS (a); $         Else thecout<<"-1"<<Endl; the     }  the     return 0; the}

Acdream 1025 BFS

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.