Luo Gu UVA12101 Prime Path problem solution

A classic BFS

Search with four for four on the line, how long to launch only change four of the water a very

  . 1 #include <the iostream>
   2 #include <CString>
   . 3 #include <cstdio>
   . 4 #include <algorithm>
   . 5 #include <Queue>
   . 6 #include <the cmath>
   . 7  the using  namespace STD;
   . 8  int T;
   . 9  int n-, m ;
 10  int Book [ 100001 ] = { 0 }; // record found over there 
. 11  struct Node // X represents the current number, step number representing the current 
12 is  {
 13 is      int X;
 14      int STEP;
15  };
 16 Queue <Node> Q; // wide search queue 
. 17  BOOL zyc_pd ( int X) // determined prime number 
18 is  {
 . 19      IF (X == 0 || == X . 1 ) return  to false ;
 20 is      the else  IF (X == 2 || == X . 3 ) return  to true ;
 21 is      the else 
22 is      {
 23 is          for ( int I = 2 ; I <= ( int ) sqrt (X); I ++ )
 24         {
 25             if(x%i==0)return false;
 26         }
 27         return true;
 28     }
 29 
 30 }
 31 void bfs() 
 32 {
 33     int x1,step1,i;
 34     while(!q.empty())
 35     {
 36         node tmp;
 37         tmp=q.front();
 38         q.pop();
 39         x1=tmp.x;//赋初始值 
 40         = tmp.step Step1; // assigned an initial value 
 41 is          // COUT X1 << << endl; 
42 is          IF (X1 == m) // if the result of the search, the output 
43 is          {
 44 is              COUT Step1 << << endl;
 45              return ;
 46 is          }
 47          for (I = . 1 ; I <= . 9 ; I + = 2 ) // bits from the odd-numbered one to nine (as is a prime number) 
48          {
 49              int YY = X1 / 10 * 10 + I; // remove search to bits plus bits 
50              IF(!! YY = X1 && Book [YY] && zyc_pd (YY)) // not the same as the number of the previous step + not repeat search + is a prime number 
51 is              {
 52 is                  Book [YY] = . 1 ; // tag is found over 
53 is                  Node TEMP ;
 54 is                  temp.x = YY; // made to the current value of 
55                  temp.step + = Step1 . 1 ; // searches plus. 1 
56 is                  q.push (TEMP);
 57 is              }
 58          }
 59          for (I = 0 ; I < = . 9 ; I ++) // ten Similarly 
60          {
 61             int yy=x1/100*100+i*10+x1%10;
 62             if(yy!=x1&&!book[yy]&&zyc_pd(yy))
 63             {
 64                 book[yy]=1;
 65                 node temp;
 66                 temp.x=yy;
 67                 temp.step=step1+1;
 68                 q.push(temp);
 69             }
 70         }
 71         for(i=0;i<=9;i++)//百位同理 
 72         {
 73             int yy=x1/1000*1000+i*100+x1%100;
 74             if(yy!=x1&&!book[yy]&&zyc_pd(yy))
 75             {
 76                 book[yy]=1;
 77                 node temp;
 78                 temp.x=yy;
 79                 temp.step=step1+1;
 80                 q.push(temp);
 81             }
 82         }
 83         for(i=1;i<=9;i++)//千位 
 84         {
 85             int yy=i*1000+x1%1000;//只换千位 
 86             if(yy!=x1&&!book[yy]&&zyc_pd(yy))
 87             {
 88                 book[yy]=1;
 89                 node temp;
 90                 temp.x=yy;
 91                 temp.step=step1+1;
 92                 q.push(temp);
 93             }
94          }
 95      }
 96      COUT << " Impossible " << endl; // if not search output Impossible 
97      return ; 
 98  }
 99  int main ()
 100  {
 101      CIN >> T;
 102      the while (T-- )
 103      {
 104          the while (q.empty ()!) q.pop (); // queue empty 
105          CIN >> >> n- m;  
 106          Memset (Book, 0 , the sizeof (Book)); //Initializing the book array 
107          book [n] = . 1 ; // the book [n] is found over the system 
108          Node tmp;
 109          tmp.x = n; // current number n 
110          tmp.step = 0 ; // current step number 0 
111          q.push (tmp);
 112          BFS ();
 113      } 
 114      return  0 ;
 115 }
Please Gangster treatise(Anyway, I do not know what that means treatise)

 

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11237436.html