Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20237   Accepted: 11282

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. 
— It is a matter of security to change such things every now and then, to keep the enemy in the dark. 
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! 
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. 
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! 
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. 
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime. 

Now, the minister of finance, who had been eavesdropping, intervened. 
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. 
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you? 
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above. 
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

Source

方法很简单,但是我写代码还是写了好久,将思路转化成代码的速度还是太慢。
其中需要素数,又复习了一下 素数筛法的方法。
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <cmath>
  6 
  7 using namespace std;
  8 
  9 bool prime[10000];
 10 int que[5000];
 11 int step[5000];
 12 int isprime[5000];
 13 bool vis[10000];
 14 int cou=0;
 15 
 16 void getprime(){
 17     int num=10000;
 18     num=sqrt(num*1.0);
 19     memset(prime,true,sizeof(prime));
 20     prime[0]=prime[1]=false;
 21     for(int i=4;i<10000;i+=2){
 22         prime[i]=false;
 23     }
 24     for(int i=3;i<num;i+=2){
 25         if(prime[i]){
 26             for(int j=i*i;j<10000;j+=2*i){
 27                 prime[j]=false;
 28             }
 29         }
 30     }
 31     for(int i=1000;i<10000;i++){
 32         if(prime[i]){
 33             isprime[cou++]=i;
 34         }
 35     }
 36 }
 37 
 38 bool judge(int a,int b){
 39     int sum=0;
 40     int t1[4],t2[4];
 41     for(int i=0;i<4;i++){
 42         t1[i]=a%10;
 43         a/=10;
 44         t2[i]=b%10;
 45         b/=10;
 46     }
 47     for(int i=0;i<4;i++){
 48         if(t1[i]==t2[i]){
 49             sum++;
 50         }
 51     }
 52     if(sum==3){
 53         return true;
 54     }else{
 55         return false;
 56     }
 57 }
 58 
 59 int bfs(int a,int b){
 60     int start=0;
 61     int endd=0;
 62     memset(vis,true,sizeof(vis));
 63     que[endd]=a;
 64     step[endd++]=0;
 65     while(start<endd){
 66         int now=que[start];
 67         int nowStep=step[start++];
 68         if(now==b){
 69             return nowStep;
 70         }
 71         for(int j=0;j<=cou;j++){
 72             int i=isprime[j];
 73             if(!vis[i]){
 74                 continue;
 75             }
 76             if(prime[i]&&judge(i,now)){
 77                 if(i==b){
 78                     return nowStep+1;
 79                 }
 80                 que[endd]=i;
 81                 step[endd++]=nowStep+1;
 82                 vis[i]=false;
 83             }
 84         }
 85     }
 86     return 0;
 87 }
 88 
 89 int main()
 90 {
 91     int n;
 92     scanf("%d",&n);
 93     int a,b;
 94     getprime();
 95     for(int i=0;i<n;i++){
 96         scanf("%d %d",&a,&b);
 97         int ans=bfs(a,b);
 98         printf("%d\n",ans);
 99     }
100     return 0;
101 }
View Code