sicily 1444 Prime Path

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

分析:

本题主要是为了找到路线的最短长度,但并不要求完整路径,而且DFS方法可能会产生指数型时间复杂度的操作数,明显应该使用BFS方法。但是,要注意到本题的一些陷阱,改变单个位置的数字产生的中间数以及初始数可能会大于结果要求的数字,所以要每位从零到九变化并且不包括原先未变化的数字。当然必须保证得到的数字都是真正的四位数。剩余要注意的就是筛法素数表和取特定位置数字,不再详述。

代码:

 1 // Problem#: 1444
 2 // Submission#: 1858029
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 #include <cstring>
 8 #include <queue>
 9 using namespace std;
10 
11 #define MAX 10000
12 #define DEBUG 1
13 
14 bool isprime[MAX];
15 bool visit[MAX];
16 struct node{
17     int value;
18     int step;
19     node( int a, int b ){
20         value = a;
21         step = b;
22     }
23 };
24 int pos[5] = {1,10,100,1000,10000};
25 
26 void sieve(){
27     memset(isprime,true,sizeof(isprime));
28     isprime[0] = isprime[1] = false;
29     for( int i=2 ; i<=100 ; i++ )
30         for( int j=i ; i*j<=MAX ; j++ )
31             if( isprime[i] ) isprime[i*j] = false;
32 }
33 
34 inline int digit( int a, int n ){
35     return (a % pos[n]) / pos[n-1]; 
36 }
37 
38 void bfs( int a, int b ){
39     queue<node> buffer;
40     buffer.push(node(a,0));
41     memset(visit,false,sizeof(visit));
42     visit[a] = true;
43     while( !buffer.empty() ){
44         node tmp = buffer.front();
45         buffer.pop();
46         if( tmp.value==b ){
47             cout << tmp.step << endl;
48             return ;
49         }
50         for( int i=3 ; i>=0 ; i-- ){
51             node next = tmp;
52             next.step++;
53             int temp = next.value - digit(next.value,i+1) * pos[i];
54             for( int j=0 ; j <= 9 ; j++ ){
55                 next.value = temp + j*pos[i];
56                 if( next.value==tmp.value || next.value<1000 ) continue;
57                 if( isprime[next.value] && !visit[next.value] ){
58                     buffer.push(next);
59                     visit[next.value] = true;
60                 }
61             }
62         }
63     }
64     cout << "Impossible" << endl;
65 }
66 
67 int main(){
68     sieve();
69     int n,a,b;
70     cin >> n;
71     while(n--){
72         cin >> a >> b;
73         bfs(a,b);
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/ciel/p/2876797.html