hdu 1973 Prime Path

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1973

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

bfs。。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::queue;
17 using std::vector;
18 using std::reverse;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int Max_N = 10010;
29 typedef unsigned long long ull;
30 int start, end;
31 namespace work {
32     struct Node {
33         int v, s;
34         Node(int i = 0, int j = 0) :v(i), s(j) {}
35     };
36     bool vis[Max_N], Prime[Max_N];
37     inline bool isPrime(int n) {
38         for (int i = 2; (ull)i * i <= n; i++) {
39             if (0 == n % i) return false;
40         }
41         return n != 1;
42     }
43     inline void init() {
44         for (int i = 1; i < Max_N; i++) {
45             Prime[i] = isPrime(i);
46         }
47     }
48     inline void bfs() {
49         char buf[10], str[10];
50         cls(vis, false);
51         queue<Node> que;
52         que.push(Node(start, 0));
53         vis[start] = true;
54         while (!que.empty()) {
55             Node tmp = que.front(); que.pop();
56             if (tmp.v == end) { printf("%d\n", tmp.s); return; }
57             sprintf(buf, "%d", tmp.v);
58             reverse(buf, buf + 4);
59             for (int i = 0; buf[i] != '\0'; i++) {
60                 rep(j, 10) {
61                     strcpy(str, buf);
62                     str[i] = j + '0';
63                     reverse(str, str + 4);
64                     int v = atoi(str);
65                     if (vis[v] || !Prime[v]) continue;
66                     que.push(Node(v, tmp.s + 1));
67                     vis[v] = true;
68                 }
69             }
70         }
71         puts("Impossible");
72     }
73 }
74 int main() {
75 #ifdef LOCAL
76     freopen("in.txt", "r", stdin);
77     freopen("out.txt", "w+", stdout);
78 #endif
79     int t;
80     work::init();
81     scanf("%d", &t);
82     while (t--) {
83         scanf("%d %d", &start, &end);
84         work::bfs();
85     }
86     return 0;
87 }
View Code
posted @ 2015-06-29 20:00  GadyPu  阅读(133)  评论(0编辑  收藏  举报