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.
— 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
每个数字共有四位,每位数字有10种可能的改变值(从0到9),但是最高位不能变为0!将问题转化为图:初始素数和所有经过一位数值改变而得到的新的素数为节点,若素数primeA经过改变后变为新的素数primeB,则A指向B!若目标素数over在图中,则开始素数至目标素数的路径上的边数ans*1即为花费的数目,否则无解!这样一来,问题就可以转化为求从素数start到素数over的最短路径了,使用BFS应该是最为合适的了!
在BFS搜索时应该设立一个数组来存目前得到的所有素数的最短路长度!!!
注意:由于题目要求多组数据输入,所以每次输入完毕后应该将队列清空,再进行广度优先搜索!!!
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int maxnum=52013;//定义常变量maxnum!
int su[maxnum],ss[maxnum],ans;
int begin,over;//定义输入端!
struct node
{
int k,len;//当前值和长度!
};
queue<node>Q;//建立队列!
void prime()//函数,筛选素数法!
{
memset(su,0,sizeof(su));
su[0]=su[1]=1;
for(int i=2; i<maxnum; i++)
if(!su[i])
for(int j=2; j*i<maxnum; j++)
su[i*j]=1;
}
int change(int num,int i,int k)
{
if(i==1)
return (num/10)*10+k;//钱加尾!
if(i==2)
return num%10+(num/100)*100+k*10;//尾加前加中!
if(i==3)
return num%100+(num/1000)*1000+k*100;
if(i==4)
return num%1000+k*1000;
}
void path()
{
node now,wow,lin;
lin.k=begin;
lin.len=0;
Q.push(lin);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(now.k==over)//找到
{
ans=now.len;
return ;
}
int knum,klen;
for(int i=1; i<=4; i++)
for(int j=0; j<10; j++)
if(!(i==4&&j==0))
{
//防止四位数的最高位被改成 0 !
knum=change(now.k,i,j);//改变后的值!
if(su[knum])
continue;//如果该数不是素数,直接排除掉!
klen=now.len+1;
if(klen>=ss[knum])
continue;//若此时路径长度不是最短的话,直接排除!
if(knum==over)
{
ans=klen;
return ;
}
ss[knum]=klen;
wow.k=knum;
wow.len=klen;
Q.push(wow);
}
}
}
int main()
{
prime();
//cout<<"测试ing……"<<endl;
int T;
cin>>T;
while(T--)
{
while(!Q.empty())
Q.pop();//清空队列!
cin>>begin>>over;
memset(ss,520,sizeof(ss));
path();
if(ans>=0)
cout<<ans<<endl;
else
cout<<"Impossible"<<endl;
}
return 0;
}