POJ 3126 Prime Path (素数筛+bfs)
题目链接:POJ 3126
| Describe: |
| 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. 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. |
| 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,将与队首元素相差一位数并且是素数的四位数入队,直到得到最小值。
AC代码:
1 #include <iostream> 2 #include <queue> 3 #define N 9999 4 using namespace std; 5 int su[N],u[N],f[N],p[N]; // f用来标记是否走过,p用来记录变化次数 6 int prime() // 玄学打表,这个cnt设置成全局变量竟然会变小,可能我的电脑中病毒了 7 { 8 int cnt = 0; // 设置成局部变量 9 for(int i = 2; i <= N; i++) 10 { 11 if(!u[i]) su[cnt++] = i; 12 for(int j = 0; j < cnt; j++) 13 { 14 if(i*su[j] > N) break; 15 u[i*su[j]] = 1; 16 if(i%su[j] == 0) break; 17 } 18 } 19 return cnt; 20 } 21 bool ok(int x, int y) // 判断两个四位数是否相差一位数 22 { 23 int c = 0; 24 while(x && y) 25 { 26 int t1 = x%10; 27 int t2 = y%10; 28 if(t1 != t2) c++; 29 x /= 10; 30 y /= 10; 31 } 32 if(c == 1) return true; 33 else return false; 34 } 35 int main() 36 { 37 int cnt = prime(); 38 int t,x,y,ans; 39 cin >> t; 40 while(t--) 41 { 42 queue<int> q; 43 ans = 9999; 44 for(int i = 0; i < N; i++) // 各种初始化 45 { 46 f[i] = 0; 47 p[i] = 0; 48 } 49 cin >> x >> y; 50 q.push(x); // 第一个元素入对 51 f[x] = 1; 52 while(!q.empty()) 53 { 54 int tmp = q.front(); q.pop(); 55 if(tmp == y) 56 { 57 ans = min(ans,p[tmp]); // 得到最小值 58 break; 59 } 60 for(int i = 0; i < cnt; i++) 61 { 62 if(su[i] > 1000 && f[su[i]] == 0 && ok(tmp,su[i])) // 过滤条件 63 { 64 q.push(su[i]); 65 f[su[i]] = 1; 66 p[su[i]] = p[tmp]+1; 67 } 68 } 69 } 70 cout << ans << endl; 71 } 72 return 0; 73 }

浙公网安备 33010602011771号