题面:
Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Problem 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.
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.
具体做法是对原始素数进行每一个位进行修改,看修改后的数是否为素数,若是则取修改次数最小的作为该素数的记录.当目标数字的值被修改一次后即可退出.这样就可以得到答案了.(最短路径是从原始素数直通目标素数,故每个素数只需要进入队列一次即可)
具体代码
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
bool check[10005];
bool vis[10005];
int answer[10005];
void sieve()
{
memset(check , true , sizeof(check));
check[1] = false;
for(int i = 1; i <= 10000; i++)//埃筛
{
if(check[i])
{
for(int j = 2; j*i <= 10000; j++)
{
check[j*i] = false;
}
}
}
}
int main()
{
sieve();
int T;
scanf("%d", &T);
while(T--)
{
memset(answer, 0, sizeof(answer));
memset(vis, false, sizeof(vis));
int start, goal;
scanf("%d%d", &start, &goal);
queue < int > q;
q.push(start);
vis[start] = true;
while(!q.empty() && !vis[goal])//当目标素数到达时及时退出
{
int temp = q.front();
q.pop();
int b = temp%10;
int t = (temp%100)/10;
int h = (temp%1000)/100;
int th = temp/1000;
for(int i = 0; i <= 9; i++)
{
int ans[4];
ans[0] = i*1000+h*100+t*10+b;
ans[1] = th*1000+i*100+t*10+b;
ans[2] = th*1000+h*100+i*10+b;
ans[3] = th*1000+h*100+t*10+i;
for(int j = 0; j < 4; j++)
{
if(check[ans[j]] && !vis[ans[j]] && ans[j] > 1000)
{
vis[ans[j]] = true;
answer[ans[j]] = answer[temp]+1;
q.push(ans[j]);
}
}
}
}
printf("%d\n", answer[goal]);
}
return 0;
}