[HDU 1973]--Prime Path(BFS,素数表)

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973

 

Prime Path

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem 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  


Source
NWERC2006     题目大意:给定两个四位素数a  b,要求把a变换到b变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数      与前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能重复。求从a到b最少需要的变换次数。无法变换则输出
Impossible   解题思路:打一个素数表,然后基于每个数的每一位bfs搜索即可,具体的可见代码~~~   代码如下:
《[HDU 1973]--Prime Path(BFS,素数表)》
《[HDU 1973]--Prime Path(BFS,素数表)》

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 struct node{
 7     int cur, step;
 8 }now, Next;
 9 int vis[10001], star, finish, prime[10001] = { 0, 0, 1 };
10 void init(){
11     for (int i = 2; i < 10001; i++){
12         if (!prime[i]){
13             for (int j = 2; i*j < 10001; j++)
14                 prime[i*j] = 1;
15         }
16     }
17 }
18 int bfs(){
19     queue<node> Q;
20     vis[star] = 1;
21     now.cur = star, now.step = 0;
22     Q.push(now);
23     while (!Q.empty()){
24         int i, j;
25         char num[5];
26         now = Q.front();
27         Q.pop();
28         if (now.cur == finish) return now.step;
29         for (i = 0; i < 4; i++){
30             sprintf(num, "%d", now.cur);
31             for (j = 0; j < 10; j++){
32                 if (j == 0 && i == 0)
33                     continue;
34                 if (i == 0)
35                     Next.cur = j * 1000 + (num[1] - '0') * 100 + (num[2] - '0') * 10 + (num[3] - '0');
36                 else if (i == 1)
37                     Next.cur = j * 100 + (num[0] - '0') * 1000 + (num[2] - '0') * 10 + (num[3] - '0');
38                 else if (i == 2)
39                     Next.cur = j * 10 + (num[0] - '0') * 1000 + (num[1] - '0') * 100 + (num[3] - '0');
40                 else if (i == 3)
41                     Next.cur = j + (num[0] - '0') * 1000 + (num[1] - '0') * 100 + (num[2] - '0') * 10;
42                 if (!prime[Next.cur] && !vis[Next.cur])
43                 {
44                     Next.step = now.step + 1;
45                     vis[Next.cur] = 1;
46                     Q.push(Next);
47                 }
48             }
49         }
50     }
51     return -1;
52 }
53 int main(){
54     int t, ans;
55     cin >> t;
56     init();
57     while (t--){
58         cin >> star >> finish;
59         memset(vis, 0, sizeof(vis));
60         ans = bfs();
61         if (ans == -1) cout << "Impossible\n";
62         else cout << ans << endl;
63     }
64     return 0;
65 }

View Code

 

其实这道题学校OJ(Swust OJ)也有但是坑爹的后台数据变成a+b的后台数据了,Orz~~~(无爱了)

 

    原文作者:繁夜
    原文地址: https://www.cnblogs.com/zyxStar/p/4580385.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞