Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17143 | Accepted: 9637 |
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
Source
题意:
两个四位数(素数),问最少经过多少次的变化能够把前者变成后者。
变换规则:每次只经过一个位上的变化,变化后的数字要求是素数并且没有在之前的过程出现过。
思路:
这都能想出来用 BFS 也是学习了。。。
卡在筛选法打的素数表上,不知道有什么 BUG 、
代码:
#include<stdio.h>
#include<queue>
#include<cstring>
#define MYDD 11030
using namespace std;
//bool prime[MYDD];//这里打出的素数表出现问题但是苦苦找不出来
//void init_pre() {//筛选法寻找素数
// memset(prime,true,sizeof(prime));
// prime[0]=prime[1]=false;
// for(int j=2; j<=MYDD; j++) {
// if(prime[j]) {
// for(int k=j*2; k<MYDD; k=k+j)
// prime[k]=false;
// }
// }
//}
bool prime[MYDD];
void init() { //对素数打表
int j,i;
for( i=1000; i<=MYDD; i++) {
for(j=2; j<i; j++)
if(i%j==0) {
prime[i]=false;
break;
}
if(j==i) prime[i]=true;
}
}
int BFS(int start,int end) {
int count[MYDD],dd[4];//记录变换的次数,存储每一位数字
int v,temp,vtemp;
bool vis[MYDD];//标记该数字是否已被使用
queue<int> q;//定义队列
memset(count,0,sizeof(count));
memset(vis,false,sizeof(vis));
q.push(start);
vis[start]=true;
while(!q.empty()) {
v=q.front();//队首元素
q.pop();
dd[0]=v/1000%10;//千位
dd[1]=v/100%10;//百位
dd[2]=v/10%10;//十位
dd[3]=v/1%10;//个位
for(int j=0; j<4; j++) {
temp=dd[j];
for(int k=0; k<=9; k++) {//组成新的数字
if(k!=temp) {
dd[j]=k;
vtemp=dd[0]*1000+dd[1]*100+dd[2]*10+dd[3];//组成的新数字
if(!vis[vtemp]&&prime[vtemp]) {//数字未被使用并且是素数
count[vtemp]=count[v]+1;
vis[vtemp]=true;//已经被访问
q.push(vtemp);//数值进队
}
if(vtemp==end)
return count[vtemp];
}
}
dd[j]=temp;
}
if(v==end)
return count[v];
}
return -1;//未找到方案
}
int main() {
int t,a,b,ans;
// init_pre();//素数表
init();
scanf("%d",&t);
while(t--) {
scanf("%d%d",&a,&b);
ans=BFS(a,b);
if(ans!=-1)
printf("%d\n",ans);
else
puts("Impossible");
}
return 0;
}
/*
4
1033 8179
1373 8017
1033 1033
*/