Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14790 | Accepted: 8341 |
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
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int start = 0; int end = 0; int ans = 0; int num = 0; //一共有多少素数 (四位数的素数) int front = 0; int back = 0; int find = 0; /*第一次提交wa 看了discuss之后还是没有找到原因,后来百度发现原来还有要考虑找不到的输出Impossible的情况*/ /*第二次提交还是wa 发现竟然把素数的个数给打印出来了。。。Omg*/ /*第三次还是wa 发现素数表可能是求错了,个数不对,我的素数总是很多,这个不对的,原来自己写的是for (j = 2; (j*j) <= i; j++) 还是写成for (j = 2; j <= (i/2); j++)然后ac的*/ #define MAXINT 10001 int map[MAXINT]; typedef struct node { int prime; int step; }nodes; nodes que[MAXINT]; int visit[MAXINT]; //找出所有四位数的素数 void getPrime() { int i = 0; int j = 0; int flag = 0; for (i = 1001; i < 9997; i++) { flag = 0; for (j = 2; j <= (i/2); j++) { if (0 == (i%j)) { flag = 1; break; } } if (0 == flag) { map[num++] = i; //printf("%d\n", i); } } //printf("num = %d\n", num);//一共有多少个素数 return; } int getright(int index, int shu, int sp) { int i = 0; int j = 0; int shuarry[4]; int mude[4]; int flag = 0; shuarry[3] = shu / 1000; shuarry[2] = (shu - shuarry[3] * 1000) / 100; shuarry[1] = (shu - shuarry[3] * 1000 - shuarry[2] * 100) / 10; shuarry[0] = shu - shuarry[3] * 1000 - shuarry[2] * 100 - shuarry[1] * 10; for (i = 0; i < num; i++) { flag = 0; if (1 == visit[map[i]]) continue; mude[3] = map[i] / 1000; mude[2] = (map[i] - mude[3] * 1000) / 100; mude[1] = (map[i] - mude[3] * 1000 - mude[2] * 100) / 10; mude[0] = map[i] - mude[3] * 1000 - mude[2] * 100 - mude[1] * 10; if (shuarry[index] == mude[index]) { continue; } for (j = 0; j < 4; j++) { if (index == j) continue; if (shuarry[j] != mude[j]) { flag = 1; break; } } //每一位是有很多的,不是单独一个的,这个很关键的 if (0 == flag) { que[back].prime = map[i]; que[back++].step = sp + 1; visit[map[i]] = 1; } } return 0; } void bfs() { int i = 0; int search = 0; que[back].prime = start; que[back++].step = 0; visit[start] = 1; while (front < back) { int x = que[front].prime; int sp = que[front].step; if (x == end) { printf("%d\n", sp); find = 1; return; } //依次找和第i不同的素数,比如i=0,就找第一位不同,但是其他位都相同的素数 for (i = 0; i < 4; i++) { search = getright(i, x, sp); if (0 != search) { } } front++; } return; } void init() { int i = 0; for (i = 0; i < MAXINT; i++) { visit[i] = 0; que[i].prime = 0; que[i].step = 0; } front = 0; back = 0; find = 0; return; } int main() { int i = 0; int T = 0; freopen("input.txt", "r", stdin); scanf("%d", &T); init(); for (i = 0; i < MAXINT; i++) { map[i] = 0; } getPrime(); for (i = 0; i<T; i++) { ans = 0; init(); scanf("%d %d", &start, &end); bfs(); if (0 == find) { printf("Impossible\n"); } } return 0; }