POJ 3126【BFS】

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
题意:
给你两个四位数的素数,通过改变其中的一位数,每次只允许改变一位数,而且改变之后的数也必须是个素数,
问你最少通过改变几次变成后一个四位的素数。如果不能改变成后面的四位素数则输出Impossible。
关键:打印素数链

因为每次只能改变一个数字 所以要把只相差一个数字的4喂数的素数连接起来
bool judge(int n, int m){
	int cnt = 0;
	while(n){
		if(n%10==m%10) cnt++;
		n /= 10;
		m /= 10;
	}
	if(cnt > 2) return true;
	return false;

}

void getMap(){
	for(int i = 1; i <= prime[0]; i++){
		for(int j = i +1; j <= prime[0];j++){
			if(judge(prime[i], prime[j])){
				map[prime[j]].push_back(prime[i]);
				map[prime[i]].push_back(prime[j]);
			}
		}
	}
}

在打印素数链前先get到素数 就是把素数存起来

bool isPrime(int n){
	for(int i = 2; i <= sqrt(n); i++){
		if(n % i == 0)
		return false;
	}
	return true;
}

void getPrime()
{
	for(int i=1000;i<10000;i++)
	{
		if(isPrime(i))
			prime[++prime[0]] = i;//这里的prime[0]表示素数的个数
	}

}

完整代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
int is_prime[10010];
int prime[10010];
int t;
int n, m;
vector<int> map[10010];
int ans;
bool vis[10010];
struct Node{
	int x;
	int step;
};
bool isPrime(int n){
	for(int i = 2; i <= sqrt(n); i++){
		if(n % i == 0)
		return false;
	}
	return true;
}
void getPrime()
{
	for(int i=1000;i<10000;i++)
	{
		if(isPrime(i))
			prime[++prime[0]] = i;
	}

}
//如果只相差1个数字 就用邻接表添加
bool judge(int n, int m){
	int cnt = 0;
	while(n){
		if(n%10==m%10) cnt++;
		n /= 10;
		m /= 10;
	}
	if(cnt > 2) return true;
	return false;

}
void getMap(){
	for(int i = 1; i <= prime[0]; i++){
		for(int j = i +1; j <= prime[0];j++){
			if(judge(prime[i], prime[j])){
				map[prime[j]].push_back(prime[i]);
				map[prime[i]].push_back(prime[j]);
			}
		}
	}
}

int bfs(){
	memset(vis, false, sizeof(vis));
	queue<Node> q;
	Node a, b;
	a.step = 0;
	a.x = n;
	vis[a.x] = true;
	q.push(a);
	while(!q.empty()){
		a = q.front();
		q.pop();
		if(a.x == m){
			 return a.step;
		}
		for(int i = 0; i < map[a.x].size(); i++){
			int tem = map[a.x][i];
			if(!vis[tem]){
				vis[tem] = true;
				b.x = tem;
				b.step = a.step + 1;
				q.push(b);
			}
		}
	}
	return -1;
}
int main(){
	getPrime();
	getMap();
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n,&m);
		ans = bfs();
		if(ans == -1)
			cout << "Impossible" << endl;
		else cout << ans << endl;
	}
	return 0;
} 
发布了28 篇原创文章 · 获赞 22 · 访问量 1047

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/102987601