Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22206 | Accepted: 12287 |
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. 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. 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 Source |
题意:给出两个质数,要求每次只能变动一位数,而且每次保证变换后的数也是质数,求最少变换次数。
解题思路:因为题目中说的很清楚,数是四位数,所以范围小了很多。对于每次变换只能改变一个数,所以在变换后不仅要判断是否为质数,还要判断是否改变的是一位数。知道这些后写起来就很简单了。这里我直接把变换的数组打了出来,72个元素(虽然蠢了点,但是直观。。)还可以用字符数组进行处理,这里我就懒得写了。我发现这个stdlib.h这个头文件里面的itoa有毒,我的编译器上能编过去,但是提交就是CE,C11标准库里面应该有这个函数吧。。然后自己写了个简易的itoa,就过了。以下是我的AC代码
#include <cstdio>
#include <iostream>
using namespace std;
#include <queue>
#include <stdlib.h>
#include <memory.h>
#include <cmath>
#include <algorithm>
#define maxn 9999
int m,n;
bool flag[maxn];
int mm[72]= {-1,-2,-3,-4,-5,-6,-7,-8,-9,1,2,3,4,5,6,7,8,9,-10,-20,-30,-40,-50,-60,-70,-80,-90,10,20,30,40,50,60,70,80,90,-100,-200,-300,-400,-500,-600,-700,-800,-900,100,200,300,400,500,600,700,800,900,-1000,-2000,-3000,-4000,-5000,-6000,-7000,-8000,-9000,1000,2000,3000,4000,5000,6000,7000,8000,9000};
bool judge(int x)
{
if(x == 0 || x == 1)
return false;
else if(x == 2 || x == 3)
return true;
else
{
for(int i = 2; i*i <= x; i++)
if(x % i == 0)
return false;
return true;
}
}
struct trip
{
int x,step;
};
void itoa(int x,int a[],int y)
{
for(int i=0;i<4;i++)
{
a[i]=x%10;
x/=10;
}
}
int check(int x,int y)
{
if(x<1000||x>9999) return 1;
int a[4],b[4];
itoa(y,b,10);
itoa(x,a,10);
int t=0;
for(int i=0;i<4;i++)
if(a[i]==b[i]) t++;
if(t!=3) return 1;
if(!judge(x)) return 1;
return flag[x];
}
void bfs()
{
memset(flag,0,sizeof(flag));
queue<trip> q;
trip now,next;
now.x=m;
now.step=0;
flag[now.x]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==n)
{
printf("%d\n",now.step);
return;
}
for(int i=0; i<72; i++)
{
next.x=now.x+mm[i];
if(check(next.x,now.x)) continue;
flag[next.x]=1;
next.step=now.step+1;
//printf("%d ",next.x);
q.push(next);
}
}
printf("Impossible\n");
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
bfs();
}
return 0;
}