Prime Path (POJ - 3126)

题面

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.

输入

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).

输出

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

样例输入

13
21033 8179
31373 8017
41033 1033

样例输出

16
27
30

提示

思路

欧拉筛打表,枚举每一位的变化,注意大于2的偶数不是质数

代码

 1using namespace std;
 2typedef long long ll;
 3const int inf = 0x3f3f3f3f;
 4const int N = 1e5+5;
 5
 6bool isp[N] = {0};
 7bool vis[N] = {0};
 8int n, s, e;
 9
10struct P {
11    int x;
12    int step;
13};
14
15void euler() {
16    int p[N], m=0;
17    for(int i=2; i<=N; i++) {
18        if(!isp[i])
19            p[m++] = i;
20        for(int j=0; j<m; j++) {
21            if(p[j]*i>N)
22                break;
23            isp[p[j]*i] = 1;
24            if(i%p[j]==0)
25                break;
26        }
27    }
28}
29
30int bfs() {
31    memset(vis, 0, sizeof(vis));
32
33    P sp;
34    sp.x = s;
35    sp.step = 0;
36
37    queue<P> q;
38    q.push(sp);
39    vis[sp.x] = 1;
40
41    while(!q.empty()) {
42        P tp = q.front();
43        q.pop();
44
45        if(tp.x==e) {
46            return tp.step;
47        }
48        P np = tp;
49        np.step = tp.step+1;
50
51        for(int i=1; i<=9; i+=2) {
52            np.x = tp.x/10*10 + i;
53            if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
54                q.push(np);
55                vis[np.x] = 1;
56            }
57        }
58        for(int i=0; i<=9; i++) {
59            np.x = tp.x/100*100 + i*10 + tp.x%10;
60            if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
61                q.push(np);
62                vis[np.x] = 1;
63            }
64        }
65        for(int i=0; i<=9; i++) {
66            np.x = tp.x/1000*1000 + i*100 + tp.x%100;
67            if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
68                q.push(np);
69                vis[np.x] = 1;
70            }
71        }
72        for(int i=1; i<=9; i++) {
73            np.x = i*1000 + tp.x%1000;
74            if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
75                q.push(np);
76                vis[np.x] = 1;
77            }
78        }
79    }
80    return 0;
81}
82
83int main(void) {
84    euler();
85    scanf("%d", &n);
86    for(int i=0; i<n; i++) {
87        scanf("%d%d", &s, &e);
88        printf("%d\n", bfs());
89    }
90    return 0;
91}