CSU---1090 数字转换问题[BFS+素数筛选]

 

1090: Number Transformation

Time Limit: 1 Sec   Memory Limit: 128 MB
SUBMIT: 387   Solved: 47
[SUBMIT] [STATUS]

Description

 In this problem, you are given a pair of integers A and B. You can transform any integer number A to B by adding x to A.This x is an integer number which is a prime below A.Now,your task is to find the minimum number of transformation required to transform S to another integer number T.

Input

 Input contains multiple test cases.Each test case contains a pair of integers S and T(0< S < T <= 1000) , one pair of integers per line. 

Output

 For each pair of input integers S and T you should output the minimum number of transformation needed as Sample output in one line. If it's impossible ,then print 'No path!' without the quotes.

Sample Input

5 7

3 4

Sample Output

Need 1 step(s)

No path!

HINT

 

Source

 

 

 

 

 

code:

  1 #include <iostream>   

  2 #include <iomanip>   

  3 #include <fstream>   

  4 #include <sstream>   

  5 #include <algorithm>   

  6 #include <string>   

  7 #include <set>   

  8 #include <utility>   

  9 #include <queue>   

 10 #include <stack>   

 11 #include <list>   

 12 #include <vector>   

 13 #include <cstdio>   

 14 #include <cstdlib>   

 15 #include <cstring>   

 16 #include <cmath>   

 17 #include <ctime>   

 18 #include <ctype.h> 

 19 using namespace std;

 20 

 21 int prime[1006];

 22 int s,t;

 23 int vst[1006];

 24 

 25 struct node

 26 {

 27     int num;

 28     int step;

 29     node(){}

 30     node(int a,int b)

 31     {

 32         num=a;

 33         step=b;

 34     }

 35 }Node;

 36 

 37 /*普通筛选法

 38 void isprime()

 39 {

 40     int i,j;

 41     memset(prime,0,sizeof(prime));

 42     for(i=2;i<=1005;i++)

 43     {

 44         if(!prime[i])

 45             for(j=i*i;j<=1005;j+=i)

 46                 prime[j]=1;

 47     }

 48 }*/

 49 

 50 //线性筛选法

 51 bool isPrime[1006];

 52 void isprime()

 53  {  

 54      memset(isPrime,true,sizeof(isPrime));  

 55      memset(prime,0,sizeof(prime));  

 56      int total=0;

 57      for(int i=2;i<=1006;i++)  

 58      {  

 59          if(isPrime[i]) 

 60              prime[total++]=i; 

 61          for(int j=0;j<total&&i*prime[j]<=1006;j++)

 62          {  

 63              isPrime[i*prime[j]]=false; 

 64               if(i%prime[j]==0) 

 65                   break; 

 66          }  

 67      }  

 68  } 

 69  

 70 

 71 int bfs()

 72 {

 73     int i;

 74     int plusnum;    

 75     memset(vst,0,sizeof(vst));

 76     vst[s]=1;

 77     node temp;

 78     temp.num=s;

 79     temp.step=0;

 80     queue<node>Que;

 81     Que.push(temp);

 82     while(!Que.empty())

 83     {

 84         temp=Que.front();

 85         Que.pop();

 86         for(i=2;i<temp.num;i++)

 87         {

 88             if(isPrime[i]==0)

 89                 continue;

 90             plusnum=temp.num+i;

 91             if(plusnum>t)

 92                 break;

 93             if(vst[plusnum])

 94                 continue;

 95             if(plusnum==t)

 96                 return temp.step+1;

 97             vst[plusnum]=1;

 98             Que.push(node(plusnum,temp.step+1));

 99         }

100     }

101     return -1;

102 }

103 

104 int main()

105 {

106     int temp;

107     isprime();

108     while(~scanf("%d%d",&s,&t))

109     {

110         temp=bfs();

111         if(temp==-1)

112             printf("No path!\n");

113         else 

114             printf("Need %d step(s)\n",temp);

115     }

116     return 0;

117 }

 普通的筛选法和线性筛选法在这题的时间相差不大,可能是数据量少的原因吧。 

 

你可能感兴趣的:(bfs)