POJ 3126-Prime Path(BFS+筛素数)

Prime Path

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 38194 Accepted: 20384

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

题目链接

题目大意:输入一个T,表示包含T组测试样例,接下来有T行,每行输入两个四位素数,每次只能变化其中一位数,并且只能变化为素数,如果能从第一个数变化到第二个数,则输出最小变化次数,如果不能,则输出impossible。

解题思路:这是POJ上的一道题目,考查BFS和素数筛法,这道题我们先把1000-10000之内的素数筛出来存放在prime数组中,true表示是个素数,false表示不是素数,这里我用的是埃氏筛法。然后输入两个数,对第一个数进行BFS,思路是这样的,每次我们能扩充N个节点,有四个方向,即个位,十位,百位,千位。每个方向又有9种选择(0-9)如果是千位则只有1-9。我们可以先将父亲节点的数拆成4个数分别存放在数组中,然后改变其中一位,有10种选择,如果改边以后的数等于原数则说明不能改,改之前判断一下就可以,扩充完成后,判断是否为素数且是否没有被扩充过,如果是则入队,每次扩之前判断一下是不是已经走到B了,如果是,则return。AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int _max=1e4+50;
bool prime[_max],book[_max];//分别定义素数数组和标记数组
int t,a,b,ans;
struct node {int num,s;};//以结构体的形式入队,num表示数,s表示步数
int main()
{
	void isprime();
	void bfs();
	cin>>t;
	memset(prime,true,sizeof(prime));//默认全为素数
	isprime();//筛素数
	while(t--)
	{
		cin>>a>>b;
		ans=-1;
		bfs();//对a进行广搜
		if(ans==-1)
		  cout<<"Impossible"<<endl;
		else
		  cout<<ans<<endl;
	}
	return 0;
}
void isprime()
{
	for(int i=2;i<=_max;i++)
	  if(prime[i])
	    for(int j=2*i;j<=_max;j+=i)//因为一个数的倍数一定不是素数,所以可以直接筛掉
	      prime[j]=false;
	for(int i=1;i<1000;i++)
	  prime[i]=false; //数据范围1e3-1e4,所以我将1e3以内的数全部置为合数
}
void bfs()
{
	int ai[5];//存放数位
	queue<node >q;
	node n;
	n.num=a;
	n.s=0;
	memset(book,false,sizeof(book));
	book[n.num]=true;//最开始只有a数走过
	q.push(n);//a先入队
	while(!q.empty())
	{
		if(q.front().num==b)
		{
			ans=q.front().s;
			return;
		}
		ai[0]=q.front().num%10;//拆数
		ai[1]=q.front().num/10%10;
		ai[2]=q.front().num/100%10;
		ai[3]=q.front().num/1000;
		for(int i=0;i<4;i++)
		{
			int num=ai[i];//每个位数依次改变
			for(int j=0;j<10;j++)
			  if(j!=num)//改后的数不能和原数相同
			  {
			  	ai[i]=j;
			  	int num1=ai[0]+ai[1]*10+ai[2]*100+ai[3]*1000;
			  	if(prime[num1]&&!book[num1])
			  	{
			  		book[num1]=true;
			  		n.num=num1;
			  		n.s=q.front().s+1;
			  		q.push(n);//符合条件入队
				}
			  }
			ai[i]=num;//最后改完一定要还原,不还原导致父亲节点不是原来的数
		}
		q.pop();//每次扩完后出队
	}
	return;
}
posted @ 2020-03-02 19:18  Hayasaka  阅读(78)  评论(0编辑  收藏  举报