POJ 3126 Prime Path

来源:互联网 发布:互联网平台运营知乎 编辑:程序博客网 时间:2024/05/29 17:53


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>using namespace std;#define N 20000int vis[N],prime[N];void prime_table(){int m=(int)sqrt(N+0.5);memset(prime,0,sizeof(prime));for(int i=2;i<=m;i++)if(!prime[i])for(int j=i*i;j<=N;j+=i)prime[j]=1;for(int i=2;i<=N;i++)if(!prime[i])prime[i]=1;else prime[i]=0;}struct point{int x,step;point(int _x,int _s):x(_x),step(_s){}point(){}};int bfs(int m,int n){memset(vis,0,sizeof(vis));queue<point>q;vis[m]=1;q.push(point(m,0));while(!q.empty()){point top=q.front();q.pop();if(top.x==n){return top.step;}int x1=top.x/1000;int x2=(top.x/100)%10;int x3=(top.x/10)%10;int x4=top.x%10;for(int k=0;k<4;k++){point temp=top;if(k==0){for(int i=1;i<10;i++){int tt=i*1000+x2*100+x3*10+x4;if(prime[tt]&&!vis[tt]){q.push(point(tt,temp.step+1));vis[tt]=1;}}}else if(k==1){for(int i=0;i<10;i++){int tt=x1*1000+i*100+x3*10+x4;if(prime[tt]&&!vis[tt]){q.push(point(tt,temp.step+1));vis[tt]=1;}}}else if(k==2){for(int i=0;i<10;i++){int tt=x1*1000+x2*100+i*10+x4;if(prime[tt]&&!vis[tt]){q.push(point(tt,temp.step+1));vis[tt]=1;}}}else if(k==3){for(int i=1;i<10;i+=2){int tt=x1*1000+x2*100+x3*10+i;if(prime[tt]&&!vis[tt]){q.push(point(tt,temp.step+1));vis[tt]=1;}}}}}return -1;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifprime_table();int m,n,t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);if(n==m){puts("0");}else{printf("%d\n",bfs(m,n));}}return 0;}


这一题纠结的挺久的,然后花了半个小时敲了一下就过了,过了。。。。。说一下题意吧,一个素数a每一次可以改变一位数字,而改变后的这个数必须是也是素数,求从A到B

最少要经过几步 可以到达。先用打出素数表加快判断速度,然后枚举各个位数的可能性,这里值得注意的是首位不能是0,从1开始枚举到9,末尾如果是偶数必定不是素数,所以可以有一个小小的优化,

for(int i=1;i<10;i+=2)


Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14659 Accepted: 8267

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

31033 81791373 80171033 1033

Sample Output

670

Source

Northwestern Europe 2006


0 0