Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1877 | Accepted: 1215 |
Description

— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
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這題主要思路就是對每一位進行0-9的改變..第一位不能位0...得到的新數入隊...循環直到找到Y
Source Code
Problem: 3126 | User: luoguangyao | |
Memory: 344K | Time: 47MS | |
Language: C++ | Result: Accepted |
- Source Code
-
1
#include <iostream>
2#include <math.h>
3#include <queue>
4#include <stdio.h>
5
6using namespace::std;
7
8int cnumber[10000];
9
10bool mark[10000];
11
12
13bool isprimer(int npp)
14{
15for (int q = 2; q <= sqrt(double(npp)); ++q)
16{
17if (npp % q == 0)
18{
19return 0;
20}
21}
22return 1;
23
24}
25
26
27int main()
28{
29
30// freopen("1.txt","r",stdin);
31
32int n;
33cin >> n;
34
35while (n)
36{
37queue<int> number;
38int x;
39int y;
40int i;
41int j;
42int newnumber;
43
44memset(cnumber,0,sizeof(cnumber));
45memset(mark,0,sizeof(mark));
46
47cin >> x >> y;
48
49
50cnumber[x] = 0;
51
52number.push(x);
53
54while (number.size())
55{
56newnumber = number.front();
57
58int p = (newnumber % 1000) / 100;
59
60int p1 = newnumber % 1000 % 100 / 10;
61
62number.pop();
63
64mark[newnumber] = 1;
65
66if (newnumber == y)
67{
68break;
69}
70
71int num;
72
73for (i = 0; i <= 9; ++i)
74{
75num = newnumber % 1000 + i * 1000;
76
77
78if (i != 0 && mark[num] != 1 && isprimer(num))
79{
80number.push(num);
81cnumber[num] = cnumber[newnumber] + 1;
82mark[num] = 1;
83}
84
85int num1;
86num1 = newnumber - p * 100 + i * 100;
87
88
89if (mark[num1] != 1 && isprimer(num1))
90{
91
92number.push(num1);
93cnumber[num1] = cnumber[newnumber] + 1;
94mark[num1] = 1;
95}
96int num2;
97num2 = newnumber - p1 * 10 + i * 10;
98
99
100if (mark[num2] != 1 && isprimer(num2))
101{
102number.push(num2);
103cnumber[num2] = cnumber[newnumber] + 1;
104mark[num2] = 1;
105}
106
107int num3 = newnumber / 10 * 10 + i;
108
109if (mark[num3] != 1 && isprimer(num3))
110{
111number.push(num3);
112cnumber[num3] = cnumber[newnumber] + 1;
113mark[num3] = 1;
114}
115}
116
117}
118
119cout << cnumber[y] << endl;
120
121n--;
122}
123
124return 0;
125}
126