Sicily 1444:prime Path (BFS)

Source: Internet
Author: User

  Test instructions to give two four-bit primes A, B, each time only a certain number of a can be modified to make it another four-bit prime, ask at least how many operations, can make a change to B. BFS search can be done directly

1#include <bits/stdc++.h>2 using namespacestd;3 4 BOOLIsPrime (intN) {//Prime Judgment5     if(n = =2|| n = =3)return true;6     Else{7         intK = sqrt (n) +1;8          for(inti =2; I < K; i++){9             if(n% i = =0)return false;Ten         } One         return true; A     } - } -  the BOOLprime[10010];  - intvisit[10010]; - voidGetprime () { -      for(inti = +; I <10000; i++){ +         if(IsPrime (i)) Prime[i] =true; -     } + } A  at structnode{ -     intNum//Storing numbers -     intCost//number of operation steps - };  -  -Node BFS (intStartintend) { inQueue<node>Q; - Node Front; toFront.num =start; +Front.cost =0; -Visit[start] =1; the Q.push (front); *      while(!Q.empty ()) { $Front =Q.front (); Q.pop ();Panax Notoginseng          -          for(inti =1; I <Ten; i++) {//Change thousands the             intm =Front.num; +m = m% ++ I * +; A             if(!visit[m] &&Prime[m]) { theVISIT[M] =1;  +Node tmp =Front; -Tmp.num =m; $tmp.cost++; $ Q.push (TMP); -                  -                 if(m = = end)returntmp; the             } -         }Wuyi          the          for(inti =0; I <Ten; i++) {//change the Hundred . -             intm =Front.num; Wum = m% -+ (m/ +) * ++ I * -; -             if(!visit[m] &&Prime[m]) { AboutVISIT[M] =1;  $Node tmp =Front; -Tmp.num =m; -tmp.cost++; - Q.push (TMP); A                  +                 if(m = = end)returntmp; the             } -         } $          the          for(inti =0; I <Ten; i++) {//Change 10 bit the             intm =Front.num; them = m%Ten+ (m/ -) * -+ I *Ten; the             if(!visit[m] &&Prime[m]) { -VISIT[M] =1;  inNode tmp =Front; theTmp.num =m; thetmp.cost++; About Q.push (TMP); the                 if(m = = end)returntmp; the             } the         } +          -          for(inti =0; I <Ten; i++) {//Change Bits the             intm =Front.num;Bayim = (m/Ten) *Ten+i; the             if(!visit[m] &&Prime[m]) { theVISIT[M] =1;  -Node tmp =Front; -Tmp.num =m; thetmp.cost++; the Q.push (TMP); the                  the                 if(m = = end)returntmp; -             } the         } the     } the Node tmp;94Tmp.num =0; theTmp.cost =0; the     returntmp; the }98 intMain () { About     intN; -CIN >>N;101 getprime ();102      while(n--){103         intA, B;104Cin >> a >>b; the Node tmp;106memset (Visit,0,sizeof(visit));107TMP =BFS (A, b);108         if(Tmp.num = =0&& Tmp.cost = =0) cout <<0<<Endl;109         Else{ thecout << tmp.cost <<Endl;111         } the     } 113}

Sicily 1444:prime Path (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.