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
8179The 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
题意:给出两个四位素数a,b。对于a每次改动一个数位上的数字并且使改动后还是一个四位素数,求至少改动多少次a才能变成b。
思路:
先素数打表。
然后建边bfs。
感觉求“至少多少次”类题目可以往图论方向想一想。
1 #include<iostream> 2 #include<cstring> 3 #include<fstream> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 #include<queue> 8 using namespace std; 9 typedef long long ll; 10 const ll mx=1e4+10; 11 ll p[mx], zs[mx], zst; 12 map<ll,ll>m; 13 ll idt; 14 vector<ll>v[mx]; 15 16 void init(){//素数 17 for(ll i=2;i<=mx;i++){ 18 if(p[i]==0) zs[++zst]=i, m[i]=zst; 19 20 for(ll j=1;j<=zst;j++){ 21 if(zs[j]*i>mx) break; 22 p[zs[j]*i]=1; 23 if(i%zs[j]==0)break; 24 } 25 } 26 } 27 void getG(){ 28 29 for(ll i=1;i<=zst;i++){ 30 ll curr=zs[i]; 31 32 ll base=1; 33 ll help=curr; 34 for(ll ws=1;ws<=4;ws++){ 35 ll num=curr-help%10*base; 36 help/=10; 37 for(ll i=0;i<=9;i++){ 38 ll nnum=num+base*i; 39 if(nnum<1000) continue;//不是四位数 40 if(nnum==curr) continue;//新数字和自己是一样的 41 if(p[nnum]==1) continue;//不是素数 42 if(count(v[m[curr]].begin(), v[m[curr]].end(), m[nnum])==0){ 43 v[m[curr]].push_back(m[nnum]); 44 } 45 if(count(v[m[nnum]].begin(), v[m[nnum]].end(), m[curr])==0){ 46 v[m[nnum]].push_back(m[curr]); 47 } 48 49 } 50 base*=10; 51 } 52 } 53 54 } 55 56 ll bfs(ll a, ll b){ 57 ll vis[mx]; 58 memset(vis, 0, sizeof(vis)); 59 queue<pair<ll, ll> >q; 60 q.push(make_pair(m[a], 0)); 61 vis[m[a]]=1; 62 while(!q.empty()){ 63 ll curr=q.front().first; 64 ll step=q.front().second; 65 66 if(curr==m[b]) return step; 67 68 q.pop(); 69 70 for(ll i=0;i<v[curr].size();i++){ 71 ll nex=v[curr][i]; 72 if(vis[nex]==1) continue; 73 q.push(make_pair(nex, step+1)); 74 vis[nex]=1; 75 } 76 77 } 78 return -1; 79 80 } 81 82 int main(){ 83 init(), getG(); 84 ll T; 85 cin>>T; 86 for(ll loop=1;loop<=T;loop++){ 87 ll a, b; 88 cin>>a>>b; 89 ll res=bfs(a, b); 90 if(res==-1) cout<<"Impossible"<<endl; 91 else cout<<res<<endl; 92 } 93 94 95 return 0; 96 }
原文:https://www.cnblogs.com/sloth612/p/13671533.html