F - Prime Path POJ - 3126
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.
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
题意:将一个素数变成另一个素数,一次只能变一位数,并且转变的过程中也要是素数。
思路:属于进阶的bfs题了,但也类似于三维的bfs。不同的地方在于,在三维bfs中所有情况只不过有六种放在一个next数组中就搞定了,这道题却有36种情况,具体看代码注释。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef struct node {
int date;
int step;
} ty;
int isp[10000];
int via[10000];
int a,b;
ty t,d;
void prime() // 先将1000到9999的素数找出来,方便之后使用。
{ // 因为有多组,如果每次都 用一个数判断一个数,肯定会超时。
memset(isp,0,sizeof(isp));
int i = 1000;
for ( i=1000; i<=9999; i++ ) {
int ci = 1;
for ( int j=2; j<i; j++ ) {
if ( i%j==0 ) {
ci = 0;
break;
}
}
if ( ci==1 ) {
isp[i] = 1;
}
else {
isp[i] = 0;
}
}
}
int bfs( int a, int b )
{
queue <ty> Q;
t.date = a;
t.step = 0;
via[a] = 1;
Q.push(t);
while ( !Q.empty() ) {
t = Q.front();
Q.pop();
if ( t.date==b ) {
return t.step;
}
int r[4];
r[0] = t.date/1000; // 千位
r[1] = t.date/100%10; // 百位
r[2] = t.date/10%10; // 十位
r[3] = t.date%10; // 个位
for ( int i=0; i<4; i++ ) { // 准备改第几位
int temp = r[i]; //每次只能变换一位数,这里用来保存该变换位的数。
for ( int j=0; j<10; j++ ) { // 第i位改成j
if ( temp!=j ) {
r[i] = j;
}
d.date = r[0]*1000+r[1]*100+r[2]*10+r[3];
d.step = t.step + 1;
if ( d.date>=1000&&d.date<=9999&&isp[d.date]==1&&via[d.date]==0 ) {
via[d.date] = 1; // 判断是否满足条件。
Q.push(d);
}
}
r[i] = temp; //变换下一位数时,该位数要恢复
}
}
return -1;
}
int main()
{
prime(); // 只打一次表就好,不要放到while里面
int i,listt,j;
cin >> listt;
while ( listt-- ) {
memset(via,0,sizeof(via));
cin >> a >> b;
int ans = bfs(a,b);
if ( ans==-1 ) {
cout << "Impossible" << endl;
}
else {
cout << ans << endl;
}
}
return 0;
}