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
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).
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
3 1033 8179 1373 8017 1033 1033
6 7 0
题意:
给你一个素数beg和一个素数end,让你把beg变成end,每次只能变一位,变化后得到的新的数也还要是素数。求最小变化次数。
分析:
因为要求最小值,所以第一个想到的应该就是bfs,每次变化一位,等到相应的素数,其步数加1。 这里为了防止重复的计算,我们应要求变化之前那个数对应的步数必须小于等于变化之后那个数对应的步数。 为什么呢? 我们记变化之前的数为cur,变化之后的数为next 。 按道理说 step[next] = step[cur] + 1 但是因为程序会做重复的计算,因此我们不能保证当前算出的next是最小的。也就是说有可能之前已经算出了通往next的更少的步数,所以当step[cur] >= step[next] 的时候,这条路我们就可以舍弃了。
注意因为要求最小值,所以我们初始化step数组为无穷大。
#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
int mypow(int a,int b)
{
int ans = 1;
for(int i=0;i<b;i++){
ans *= a;
}
return ans;
}
int change(int temp,int i,int j)
{
int key = temp/mypow(10,i)%10;
temp = temp - key*mypow(10,i) + j*mypow(10,i);
return temp;
}
int beg,en;
bool prime[10005]={0};
int main()
{
for(int i=2;i<10005;i++){
if(prime[i] == 0)
for(int j=i;j*i<10005;j++){
prime[i*j] = 1;
}
}
int T;
cin >> T;
int step[10005]; //store the steps from begin to current.
while(T--){
memset(dp,0x3f,sizeof(dp)); // to find the minimum step, we need to initialize the step INF.
cin >> beg >> en;
queue<int> q;
q.push(beg);
dp[beg] = 0;
while(!q.empty()){
int temp = q.front();
q.pop();
for(int i=0;i<4;i++){
for(int j=0;j<10;j++){
if(i==3 && j==0) continue;
int next = change(temp,i,j);
if(prime[next] == 0 && step[next] > step[temp]){ //The second condition avoids plenty of repeated calculations.
q.push(next);
step[next] = step[temp]+1;
}
}
}
}
cout << step[en] << endl;
}
}