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
题意:由第一个数每次改变一位,改成第二个数需要几步(两个数都是素数)
本题其实就是在找初始素数至目标素数的最短路径,应该使用优先搜索,本题其实没有必要用队列,因为队列自动排序的特点根本就是没有用的,用数组就够了,但是可能会增加变量来控制当前入队的情况,有时间用数组做一下........
本题注意
(1)本题是图的性质,而不是树
(2)由于数据比较庞大,应该首先考虑离线求素数的方式(筛法)
(3)使用queue时,要注意queue的特点,是在遍历每一个当前值得时候,都将值压入当前的分支内,而不是单独的,所以遍历的时候注意遍历的顺序(见后面插图)
是一个节点一个节点的访问(包括当前节点的孩子)
(4)本人认为本题的精髓,也是作为与树区别最大的特点就是加了一个visit[]数组,作为标记是否已经访问过的标记,这样的话,一方面不会造成程序的死循环,另一方面也会满足本题最小值的条件,因为在前面已经访问过的话,肯定比当前的小
#include <iostream> #include <stdio.h> #include <queue> #include <math.h> #include <string.h> #define N 10010 using namespace std; bool isPrm[N],visit[N];//isPrm[N]是用来记录是否为素数的,visit[]是用来记录是否已经遍历过 int path_c[N]; //记录路径的长度 void getPrm(){//求素数法(快速筛法) int s,e=sqrt((double)N)+1;//sqrt是对于double数开平方 memset(isPrm,1,sizeof(isPrm)); isPrm[0]=isPrm[1]=0; for(int i=4;i<N;i+=2)isPrm[i]=0; for(int i=3;i<e;i+=2) if(isPrm[i]) for(int s=i*2,j=i*i;j<N;j+=s) isPrm[j]=0; } int main(){ int n,m,t; getPrm(); scanf("%d",&t); while(t--){ int mark=0; queue<int> q; cin>>n>>m; memset(visit,false,sizeof(visit)); memset(path_c,0,sizeof(path_c)); q.push(n);//第一个数据压入队列中 visit[n]=true;//该点遍历过 while(!q.empty()){ if(mark==1) break; int temp=q.front();//取出第一个,出队元素 q.pop();//弹出 if(temp==m) break;//直至等于m,停止 for(int i=1;i<=1000;i=i*10) {//4个位置,个十百千位上一次为0-9的BFS的遍历,i决定着当前是分离哪位 if(mark==1) break; int d=temp/i%10,w=temp-d*i; //将当前位的数字分离出来,d是被分离出来位上的数 //去掉该位以后的数 for(int j=0;j<10;j++){ //每一位上0-9的BFS的遍历,j是要替换当前位的数 if(i==1000&&j==0) continue;//最高位不为0 if(j!=d){ //不是原来的那个位上的数 int now=j*i+w; //产生一个新的数 if(isPrm[now]&&!visit[now]){//isPrm[now]如果是素数,且!visit[now]没有被访问过 q.push(now);//入队 path_c[now]=path_c[temp]+1;//在原来的基础上加1,标志走过的路径长度 if(now==m){ mark=1; break; } visit[now]=true;//标志访问过 } } } } } printf("%d\n",path_c[m]); } return 0; }
注意两种方式的对比,下面的这种情况本人感觉不是很好理解,因为在找出要找的数值之后还会继续查找直到访问该节点的才会停止,其实是早就遇到当前的值了,没有必要还在遇到要找的值之后还继续,直到访问到才结束,还是当面的好一点,感觉自己写的比较简洁省时
int main() { int n,m,t; getPrm(); scanf("%d",&t); while(t--) { int mark=0; queue <int> q; cin>>n>>m; memset(visit+900 ,false,sizeof(visit)); memset(path_c+900,0,sizeof(path_c)); q.push(n); visit[n]=true; while(!q.empty()) { if(mark==1) break; int temp=q.front(); q.pop(); if(temp==m) break; for(int i=1;i<=1000;i=i*10) { if(mark==1) break; int d=temp; d=d/i; d=d%10; int w=temp-d*i; for(int j=0;j<10;j++) { if(i==1000&&j==0) continue; if(j!=d) { int now=j*i+w; if(isPrm[now]&&!visit[now]) { q.push(now); path_c[now]=path_c[temp]+1; if(now==m) { mark=1; break; } visit[now]=true; } } } } } printf("%d\n",path_c[m]); } return 0; }