Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10344 | Accepted: 5907 |
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
题意:
给出 N (<= 100)个 case,后每个 case 都给出两个四位数 A 和 B ,问最少需要将 A 经过多少次变化才能变成 B,每次变化只能变一位数,且每次得出来的数都必须为素数。
思路:
数学 + 搜索。首先先预处理好素数,再保存1000 ~ 9999之间的素数,并每个素数都对应着一个编号,并且转化成图,图用邻接表保存,每个数都有一个出度,指向的是与其相差一位的数。后给出 A 和 B 就是寻找 A 到 B 的最短路,故用 BFS 找最短路即可,若不预处理事先转化成素数图,在 BFS 中再一个个找的话,无疑会 TLE 的。
AC:
#include <cstdio> #include <queue> #include <map> #include <string.h> #define MAX 10000 using namespace std; typedef struct { int num,step; }node; node no[MAX]; int pri[MAX],pri_num[MAX]; int fir[MAX],next[MAX * 2],v[MAX * 2]; int vis[MAX]; int pri_sum,ind; void add_edge(int f,int t) { v[ind] = t; next[ind] = fir[f]; fir[f] = ind; ind++; } bool test(int a,int b) { int sum = 0; while(a && b) { if(a % 10 != b % 10) sum++; if(sum >= 2) return false; a /= 10; b /= 10; } return true; } void solve() { memset(pri,0,sizeof(pri)); memset(fir,-1,sizeof(fir)); pri_sum = 0; ind = 0; pri[1] = 1; for(int i = 2;i * i < MAX;i++) { if(pri[i]) continue; for(int j = i;j * i < MAX;j++) pri[i * j] = 1; } for(int i = 1000;i <= 9999;i++) if(!pri[i]) pri_num[pri_sum++] = i; for(int i = 0;i < pri_sum;i++) for(int j = i + 1;j < pri_sum;j++) { if(test(pri_num[i],pri_num[j])) { add_edge(i,j); add_edge(j,i); } } } int BFS(int st,int en) { if(st == en) return 0; queue<node> q; memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); node now; now.num = st;now.step = 0; q.push(now); vis[st] = 1; while(!q.empty()) { now = q.front();q.pop(); int num = now.num,step = now.step; //printf("%d %d\n",pri_num[num],step); for(int e = fir[num];e != -1;e = next[e]) { int vn = v[e]; if(!vis[vn]) { node in; in.num = vn;in.step = step + 1; if(vn == en) return in.step; vis[vn] = 1; q.push(in); } } } return -1; } int main() { int n; solve(); scanf("%d",&n); while(n--) { int s,e,res; scanf("%d%d",&s,&e); for(int i = 0;i < pri_sum;i++) { if(pri_num[i] == s) s = i; if(pri_num[i] == e) e = i; } res = BFS(s,e); if(res == -1) printf("Impossible\n"); else printf("%d\n",res); } return 0; }