POJ 3126 Prime Path(BFS + 素数打表)
生活随笔
收集整理的這篇文章主要介紹了
POJ 3126 Prime Path(BFS + 素数打表)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給定兩個四位素數, 從一個素數到另一個素數,最少用幾步,可以一次更改四位中的任意一位,但每次改變都只能是素數。
解題思路:四位數每一位情況有十種情況0-9, 四位共有40種情況, 枚舉40種情況,拿出來判斷是不是素數表中的,如果是就壓入隊列,不是就拋棄。
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 1033Sample Output
6 7 0 #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<list> #include<iostream> #include<map> #include<queue> #include<set> #include<stack> #include<vector>using namespace std; typedef pair<int, int> P; #define MAX_N 10000 const int INF = 0x3f3f3f3f; int n , m; bool prime[MAX_N]; bool vis[MAX_N]; int d[MAX_N];//void make_prime() //{ // memset(prime, true, sizeof(prime)); // for(int i = 1000 ; i <= MAX_N ; i++) // { // for(int j = 2 ; j < i ; j++) // { // if(i % j == 0) // { // prime[i] = false; // break; // } // } // }for(int i = 1000 ; i <= MAX_N ; i++)if(prime[i] == true) cout<<i<<endl; //}void init() //對素數打表 {int i,j;for(i=1000; i<MAX_N; i++){for(j=2; j<i; j++)if(i%j==0){prime[i]=false;break;}if(j==i) prime[i]=true;} }int bfs(int n ,int m) {memset(vis, false, sizeof(vis));memset(d, -1, sizeof(d));queue<int> que;vis[n] = true;d[n] = 0;que.push(n);while(!que.empty()){int p = que.front();que.pop();if(p == m)return d[p];int t[4];t[0] = p / 1000;t[1] = p % 1000 / 100;t[2] = p % 100 / 10;t[3] = p % 10;for(int i = 0 ; i < 4 ; i++){int tmp = t[i];for(int j = 0 ; j < 10 ; j++){if(j != tmp){t[i] = j;int num = t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];if(num >= 1000 && !vis[num] && prime[num]){ // cout<<num<<endl;d[num] = d[p] + 1;vis[num] = true;que.push(num);}}}t[i] = tmp;}}return -1; }int main() {init(); // freopen("2.txt","w",stdout); // for(int i = 0 ; i < MAX_N ; i++) // if(prime[i]) // cout<<i<<endl;int t;scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);int ans = bfs(n, m);if(ans == -1)cout<<"Impossible"<<endl;elsecout<<ans<<endl;}return 0; }總結
以上是生活随笔為你收集整理的POJ 3126 Prime Path(BFS + 素数打表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吉林警方重击涉黑涉恶犯罪 一年打掉439
- 下一篇: NSString 和 UInt8 相互转