Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16991 | Accepted: 9561 |
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这道题就是说给你n和m让你从n转化到m,n可以每次改变每个位置上的数,使之成为一个新的数,这里全程都是素数(给出的是,转化时也必须是),问你需要转化的最少步数,如果不可能转化成,那么输出impossible。
这种题也是从初始状态转化为目标状态的题,用bfs然后每次转化就是改变某一位上的数,所以状态就是这个素数。
所以搜索的时候就是从初始状态每次改变某一个位置上的数,如果这个数是素数并且没有被搜索过,就进入队列。
详细 的看代码吧
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN=10000+5;
bool vis[MAXN];
bool judge(int x)//判断素数
{
int i;
for(i=2;i<=sqrt(x);++i)
{
if(x%i==0)return 0;
}
return 1;
}
struct node
{
int key;
int step;
};
int bfs(int s,int e)
{
memset(vis,0,sizeof(vis));
node k,t;
queue<node>q;
k.key=s;
k.step=0;
q.push(k);//初始化状态
vis[k.key]=1;
int i;
while(!q.empty())
{
k=q.front();
q.pop();
if(k.key==e)return k.step;
t.key=k.key/10*10; //去掉个位,之后每次改变个位
for(i=0;i<=9;++i)
{
if(!vis[t.key]&&judge(t.key))
{
vis[t.key]=1;
t.step=k.step+1;
q.push(t);
}
t.key++;
}
t.key=k.key/100*100+k.key%10;//去掉十位,然后每次改变10位
for(i=0;i<=9;i++)
{
if(!vis[t.key]&&judge(t.key))
{
vis[t.key]=1;
t.step=k.step+1;
q.push(t);
}
t.key+=10;
}
t.key=k.key/1000*1000+k.key%100;//去掉百位,每次改变百位
for(i=0;i<=9;i++)
{
if(!vis[t.key]&&judge(t.key))
{
vis[t.key]=1;
t.step=k.step+1;
q.push(t);
}
t.key+=100;
}
t.key=k.key%1000;//去掉千位,每次改变千位
for(i=1;i<=9;++i)
{
t.key+=1000;
if(!vis[t.key]&&judge(t.key))
{
vis[t.key]=1;
t.step=k.step+1;
q.push(t);
}
}
}
return -1;
}
int main()
{
int t;
int s,e;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
int ans=bfs(s,e);
if(ans==-1)printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}