Prime Path
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 12060 |
|
Accepted: 6843 |
Description
The ministers of the Cabinet were quite upset by the message from the chief of Security stating, they would all has T o Change the Four-digit-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 had 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'll just has to paste four new digits over the four old ones on your office door.
-no, it ' s not so simple. Suppose that I change the first digit to an 8 and then the number would read 8033 which is not a prime!
-I see, being the Prime Minister you cannot stand have 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 the prime numbers where only one digit are 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 then the price of a digit is one pound.
-HMM, in this case I need a computer program to minimize the cost. You don ' t know some very cheap software gurus?
-in fact, I do. You see, there are this programming contest going on ... Help the Prime minister to find the cheapest prime path between any and given Four-digit primes! The first digit must be nonzero, of course. Here's 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 step 2 can is reused in the last Step–a new 1 must is purchased.
Input
One line with a positive number:the number of test cases (at most 100). Then for each test case, one line with the numbers separated by a blank. Both numbers is four-digit primes (without leading zeros).
Output
Either with a number stating the minimal cost or containing the word impossible.
Sample Input
31033 81791373 80171033 1033
Sample Output
670
Transmission Door
Test instructions: There is a prime number of people to change the four-digit numbers, give the old number and the new house, each transformation can only change one count, and he does not see with Xu is not a prime number of the four-digit appearance, the minimum number of changes can be changed to allow the old house numbers for new house numbers .
Idea: BFS, each transformation can only change one number, so to each digit processing, and remember to mark, the queue of the book on the mark, do not join the team. Only the number of the queue, so every time before the team to determine whether the prime consumption is too large, directly hit a prime table, 1~10000 can, sieve method to maintain the state of the Mark form, not the prime number is marked 0.
612 KB0 ms#include<cstdio> #include <cstring> #include <queue>using namespace Std;int _,flag;int A, b ; int prime[10010];bool book[10010];queue<pair<int,int> >que;void ini () {flag=0; memset (book,0,sizeof (book)); scanf ("%d%d", &a,&b); while (!que.empty ()) Que.pop ();} void BFs () {Que.push (Make_pair (a,0)); Book[a]=1; while (!que.empty ()) {pair<int,int> t=que.front (); Que.pop (); if (t.first==b) {printf ("%d\n", t.second); Flag=1;return;} int X=t.first; for (int i=1;i<=9;i++) {int nx=x%1000+i*1000;//only transforms the thousand number if (Prime[nx]&&!book[nx]) {Que.push (M Ake_pair (nx,t.second+1)); Book[nx]=1;} } for (int i=0;i<=9;i++) {int nx=x%100+1000* (x/1000) +i*100;//change only the Hundred count if (prime[nx]&& !BOOK[NX]) {Que.push (Make_pair (nx,t.second+1)); book[nx]=1;} } for (int i=0;i<=9;i++) {int nx=x%10+100* (x/100) +i*10;//Transform only 10-bit number if (primE[NX]&&!BOOK[NX]) {Que.push (Make_pair (nx,t.second+1)); book[nx]=1;} } for (int i=0;i<=9;i++) {int nx=10* (X/10) +i;//only transforms single-digit if (Prime[nx]&&!book[nx]) {que . Push (Make_pair (nx,t.second+1)); Book[nx]=1;} }}}int Main () {memset (prime,-1,sizeof (prime)); /* Prime Sieve */for (int i=2;i<=10;i++)//Reject all non-prime for (int j=2;j*j<=i;j++) if (i%j==0) prime[i]=0 of 10 or less; prime[1]=0; for (int i=1;i<=10;i++)//Reject all non-primes within 100, and by the way remove some non-prime if (Prime[i]) for (int j=2;i*j<=10000;j+) between 100~10000 +) prime[i*j]=0; for (int i=11;i<=100;i++)//Reject all non-prime if (Prime[i]) for (int j=2;i*j<=10000;j++) prime[i between 100~10000 *j]=0; scanf ("%d", &_); while (_--) {ini (); BFS (); if (!flag) printf ("impossible\n"); } return 0;}
POJ 3126 Prime Path (BFS)