Prime path
— 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
//
本人第一次在csdn上写博客,有点小激动哈^-^如有错误,有请大佬们指正!
题解:本题是一道bfs问题,但并不是非常明显,需要稍微对题目进行分析。下面放代码,详细说明见代码注释。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int t;
int m,n;
int prime[10000]; //用于判断是否为素数
int que[10000][2]; //que[x][0]用于存放符合要求的素数,que[x][1]用于存放该素数被找到所用的步数
bool p[10000]; //用于判断某个素数是否已经入队
void bfs(){
que[1][0]=m; //队列初始化
que[1][1]=0;
int h=0;
int t=1;
int x;
do
{
h++;
if (que[h][0]==n)
{
cout<<que[h][1]<<endl; //如果找到目标数字,则输出步数并停止搜索
return ;
}
for (int i=1;i<=9;i++) //改变千位的数字
{
x=i*1000+que[h][0]-que[h][0]/1000*1000;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=0;i<=9;i++) //改变百位的数字
{
x=i*100+que[h][0]/1000*1000+que[h][0]%100;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=0;i<=9;i++) //改变十位的数字
{
x=i*10+que[h][0]/100*100+que[h][0]%10;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
for (int i=1;i<=9;i+=2) //改变个位的数字
{
x=que[h][0]/10*10+i;
//cout<<x<<endl;
if (prime[x]&&!p[x])
{
t++;
que[t][0]=x;
que[t][1]=que[h][1]+1;
p[x]=1;
}
}
}while(h<t);
cout<<"Impossible"<<endl; //如果全部找过了也没有目标数字,则输出Impossible
}
int main()
{
prime[2]=prime[3]=1;
//这是一种快捷的素数打表方法(之前在一篇博客上看到的,但不记的博 主的名字了),规律是除2,3以外,所有的素数要么是6的倍数加1,要么减1;
for (int i=5;i<10000;i++)
if ((i+1)%6==0||(i-1)%6==0)
{
int j;
for (j=2;j<(int)(sqrt(i)+1);j++)
{
if (i%j==0)
break;
}
if (j==(int)(sqrt(i)+1))
prime[i]=1;
}
}
cin>>t;
while(t--)
{
memset(p,0,sizeof(p));
cin>>m>>n;
bfs();
}
}
//如果有什么建议,欢迎在评论区留言!