【题解】POJ-3126 Prime Path

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.

样例输入

1
2
3
4
3
1033 8179
1373 8017
1033 1033

样例输出

1
2
3
6
7
0

提示

思路

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5+5;

bool isp[N] = {0};
bool vis[N] = {0};
int n, s, e;

struct P {
int x;
int step;
};

void euler() {
int p[N], m=0;
for(int i=2; i<=N; i++) {
if(!isp[i])
p[m++] = i;
for(int j=0; j<m; j++) {
if(p[j]*i>N)
break;
isp[p[j]*i] = 1;
if(i%p[j]==0)
break;
}
}
}

int bfs() {
memset(vis, 0, sizeof(vis));

P sp;
sp.x = s;
sp.step = 0;

queue<P> q;
q.push(sp);
vis[sp.x] = 1;

while(!q.empty()) {
P tp = q.front();
q.pop();

if(tp.x==e) {
return tp.step;
}
P np = tp;
np.step = tp.step+1;

for(int i=1; i<=9; i+=2) {
np.x = tp.x/10*10 + i;
if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
q.push(np);
vis[np.x] = 1;
}
}
for(int i=0; i<=9; i++) {
np.x = tp.x/100*100 + i*10 + tp.x%10;
if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
q.push(np);
vis[np.x] = 1;
}
}
for(int i=0; i<=9; i++) {
np.x = tp.x/1000*1000 + i*100 + tp.x%100;
if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
q.push(np);
vis[np.x] = 1;
}
}
for(int i=1; i<=9; i++) {
np.x = i*1000 + tp.x%1000;
if(np.x!=tp.x && !vis[np.x] && !isp[np.x]) {
q.push(np);
vis[np.x] = 1;
}
}
}
return 0;
}

int main(void) {
euler();
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%d%d", &s, &e);
printf("%d\n", bfs());
}
return 0;
}