素数打表)"/>
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.
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
#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 + 素数打表)
发布评论