链接:戳这里
Prime Path
Time Limit: 1000MS
Memory Limit: 65536K
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
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
题意:
两个1000~9999的素数x,y,要求在改变千、百、十、个位上的数字得到一个新的素数
问最少经过几次变换可以使得x->y 注意全程的数都必须是素数
思路:
处理一下1000~9999的素数,并标记
然后暴力改成新的数再去判断 bfs
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include <ctime> #include<queue> #include<set> #include<map> #include<stack> #include<iomanip> #include<cmath> #define mst(ss,b) memset((ss),(b),sizeof(ss)) #define maxn 0x3f3f3f3f #define MAX 1000100 ///#pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; typedef unsigned long long ull; #define INF (1ll<<60)-1 using namespace std; int mu[1000100],prime[1000100],vis[1000100],a[1000100]; struct node{ int v,step; node(int v=0,int step=0):v(v),step(step){} }; int used[1000100]; void BFS(int x,int y){ mst(used,0); queue<node> qu; qu.push(node(x,0)); used[x]=1; while(!qu.empty()){ node now=qu.front(); qu.pop(); if(now.v==y){ cout<<now.step<<endl; return ; } for(int i=0;i<4;i++){ for(int j=0;j<=9;j++){ if(i==0 && j==0) continue; if(i==0){ int tmp=now.v%1000+j*1000; ///printf("%d %d %d %d\n",i,j,now.v,tmp); if(!used[tmp] && vis[tmp]){ used[tmp]=1; qu.push(node(tmp,now.step+1)); } } else if(i==1){ int tmp=now.v%100+j*100+(now.v/1000)*1000; if(!used[tmp] && vis[tmp]){ used[tmp]=1; qu.push(node(tmp,now.step+1)); } } else if(i==2){ int tmp=now.v/100*100+j*10+now.v%10; if(!used[tmp] && vis[tmp]){ used[tmp]=1; qu.push(node(tmp,now.step+1)); } } else { int tmp=now.v/10*10+j; if(!used[tmp] && vis[tmp]){ used[tmp]=1; qu.push(node(tmp,now.step+1)); } } } } } } int main(){ mu[1]=1; int cnt=0,num=0; for(int i=2;i<=100000;i++){ if(!vis[i]){ prime[cnt++]=i; mu[i]=-1; if(i>=1000 && i<10000) a[num++]=i; } for(int j=0;j<cnt;j++){ if(i*prime[j]>100000) break; vis[i*prime[j]]=1; if(i%prime[j]==0){ mu[i*prime[j]]=0; break; } else mu[i*prime[j]]=-mu[i]; } } mst(vis,0); for(int i=0;i<num;i++) vis[a[i]]=1; int T; scanf("%d",&T); while(T--){ int x,y; scanf("%d%d",&x,&y); BFS(x,y); } return 0; }