1444. Prime Path
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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.
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
题目分析
题目大意是给定两个素数s和e,在s变成e的时候,每次只能替换一位数字,要求变换过程中的数字也必须是素数且首位不为0.求最短的步数
典型的广搜题目,注意两点
一是在依位产生新的数字时,要注意保存原来的数字,确保每次只替换了一个位的值
二是做好访问标记,题目是1000-9999,所以可以直接开数组存储,时间是0.03s,内存是348kb,
我用vector做的时候,时间是0.05s,内存是340kb
#include <cstdio>
#include <cmath>
#include <memory.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
//std::vector<int> visited;
int visited[10000];
bool isPrime(int data) {
for (int i = 2; i <= sqrt(data); i++)
if (data % i == 0)
return false;
return true;
}
bool visit(int data) {
// std::vector<int>::iterator result = find(visited.begin( ), visited.end( ), data);
// return result != visited.end();
return visited[data];
}
int main() {
int num;
scanf("%d", &num);
while (num--) {
int start, end;
scanf("%d%d", &start, &end);
if (start == end) {
printf("0\n");
continue;
}
// if (!visited.empty())
// visited.clear();
memset(visited, 0, sizeof(visited));
std::queue<int> sol;
std::queue<int> len;
bool flag = false;
int ans = 0;
sol.push(start);
len.push(0);
// visited.push_back(start);
visited[start] = 1;
while (!sol.empty() && !flag) {
int nowdata = sol.front();
int nowlen = len.front();
sol.pop();
len.pop();
int digit[4];
digit[0] = nowdata / 1000;
nowdata %= 1000;
digit[1] = nowdata / 100;
nowdata %= 100;
digit[2] = nowdata / 10;
digit[3] = nowdata % 10;
int temp, init;
for (int i = 0; i < 4; ++i) {
init = digit[i];
if (flag)
break;
for (int j = i==0?1:0; j < 10; ++j) {
if (flag)
break;
digit[i] = j;
temp = digit[0]*1000 + digit[1]*100 + digit[2]*10 + digit[3];
if (temp == end) {
ans = nowlen+1;
flag = true;
break;
}
if (isPrime(temp) && !visit(temp)) {
sol.push(temp);
len.push(nowlen+1);
// visited.push_back(temp);
visited[temp] = 1;
}
}
digit[i] = init;
}
}
if (flag)
printf("%d\n", ans);
else
printf("Impossible\n");
}
}