【题目描述】
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.
内阁部长们对安全局局长的信息感到非常不安,他说,他们都必须改变他们办公室的四位数的房间号码。
— 不时地改变这种情况,使敌人处于黑暗之中是一个安全的问题不时地改变这种情况。
— 但是听着,我选择我的号码1033是有充分理由的。我是首相,你知道的!
— 我知道,所以你的新号码8179也是素数。你只需要把四个新的数字贴在办公室门上的四个旧的上面。
— 不,没那么简单。假设我将第一个数字改为8,那么这个数字将读到8033,它不是素数!
— 我明白了,作为首相,你连几秒钟都不能忍受门上有一个非素数。
— 对,是这样!所以我必须发明一个从1033到8179的素数路径,其中只有一个数字从一个素数变为下一个素数。
现在,一直在偷听的财政部长进行了干预。
— 请不要花不必要的钱!我碰巧知道一位数的价格是一英镑。
— 嗯,在这种情况下,我需要一个计算机程序,以尽量减少成本。你不认识一些很便宜的软件专家,是吗?
— 事实上,我知道。你看,有个节目比赛正在进行.帮助首相找到任何两个给定的四位数素数之间最便宜的主要路径!当然,第一个数字必须是非零。以下是上述情况下的解决方案。
1033
1733
3733
3739
3779
8779
8179
这个解决方案的费用是6英镑。注意,在第二步中粘贴的数字1不能在最后一步重复使用-必须购买新的1。
【输入】
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).
第一行一个正整数表示测试数量(最大为100)
后面每个测试数据每行两个用空格隔开的数字,每个数字都是四位素数(没有前导0)
【输出】
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
每个测试数据一行,输出在此情况下的最小花费。若不存在从m到n的路径,则输出单词“Impossible”。
【样例输入】
3
1033 8179
1373 8017
1033 1033
【样例输出】
6
7
0
题目链接:https://cn.vjudge.net/problem/POJ-3126
代码如下:
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
static const int MAXN=10000;
int prime[MAXN];
int vis[MAXN];
struct Node{
int num;
int step;
};
void Prime()//素数筛
{
memset(prime,0,sizeof(prime));
prime[0]</