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
题目意思是让你看第一个四位数变到第二个四位数要多少步,其中每变一步改动一个数字,改完数字之后的新的数还得是质数,求最小的改变次数,还有什么犹豫不决吗?果断广搜解决问题啊!!!
广搜重在理解过程,如果理解了会觉得非常简单!(虽然代码打起来复杂,我打个比方就是深搜是美女,却没内涵,广搜长得其丑无比,可是人家内涵比深搜丰富多了!!)
#include <iostream>
#include<memory.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
bool p[100000],vis[10000];
int a[4],b[4];
struct data
{
int n,num;
}x[100000];
void get()
{
memset(p,1,sizeof(p));
for(ll i=2;i<100000;i++)
{
if(p[i]==1)
for(ll j=i*i;j<100000;j+=i)
p[j]=0;
}
}
int hh(int *a)
{
return a[0]*1000+a[1]*100+a[2]*10+a[3];
}
int bfs(int *a,int *b)
{
if(hh(a)==hh(b)||p[hh(b)]==0)return 0;
int l=0,r=0;
vis[hh(a)]=1;
x[r].n=hh(a);
x[r++].num=0;
while(l<r)
{
int nn=x[l].n;
int c[4];
c[0]=nn/1000,c[1]=(nn/100)%10,c[2]=(nn/10)%10,c[3]=nn%10;
for(int i=0;i<4;i++)
{
if(i==0)
{
int tem=c[0];
for(int j=1;j<10;j++)
{
c[0]=j;
if(vis[hh(c)]==1)continue;
if(p[hh(c)]==0)continue;
if(hh(c)==hh(b))
return x[l].num+1;
// if(hh(c)==3733||hh(c)==3739||hh(c)==3779)
// cout<<"finding!!!!!!!!千位"<<endl;
// cout<<hh(c)<<' '<<x[l].num+1<<endl;
vis[hh(c)]=1;
x[r].num=x[l].num+1;
x[r].n=hh(c);r++;
}c[0]=tem;
}
else if(i==3)
{
int tem=c[3];
for(int j=1;j<10;j+=2)
{
c[3]=j;
if(vis[hh(c)]==1)continue;
if(p[hh(c)]==0)continue;
if(hh(c)==hh(b))
return x[l].num+1;
// if(hh(c)==3733||hh(c)==3739||hh(c)==3779)
// cout<<"finding!!!!!!!!!!!个位"<<endl;
// cout<<hh(c)<<' '<<x[l].num+1<<endl;
vis[hh(c)]=1;
x[r].num=x[l].num+1;
x[r].n=hh(c);
r++;
}
c[3]=tem;
}else
{
int tem=c[i];
for(int j=0;j<10;j++)
{
c[i]=j;
if(vis[hh(c)]==1)continue;
if(p[hh(c)]==0)continue;
if(hh(c)==hh(b))
return x[l].num+1;
// if(hh(c)==3733||hh(c)==3739||hh(c)==3779)
// cout<<"finding!!!!!!!!!!!!"<<endl;
// cout<<hh(c)<<' '<<x[l].num+1<<endl;
vis[hh(c)]=1;
x[r].num=x[l].num+1;
x[r].n=hh(c);r++;
}
c[i]=tem;
}
}
l++;
}
return -1;
}
int main()
{
get();
int c;
cin>>c;
while(c--)
{
memset(vis,0,sizeof(vis));
int a0,b0;
cin>>a0>>b0;
a[0]=a0/1000,a[1]=(a0/100)%10,a[2]=(a0/10)%10,a[3]=a0%10;
b[0]=b0/1000,b[1]=(b0/100)%10,b[2]=(b0/10)%10,b[3]=b0%10;
int ss=bfs(a,b);
if(ss==-1)cout<<"Impossible"<<endl;
else cout<<ss<<endl;
}
return 0;
}