Poj3126--bfs

Source: Internet
Author: User

POJ 1326-digit bfsprime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12480 Accepted: 7069

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

Test instructions: The shortest number of steps required from a four-digit prime number to another four-digit prime number, each step can only change one digit, and the number of changes must be four-digit number
This is a typical digital search problem, to find the shortest circuit so with BFS, the difficulty lies in the conversion of analog number, and also pay attention to the border control
#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intmaxn=10000;intn,m;BOOLVIS[MAXN];intans;structnode{intNum,step;};intMypow (intNintk) {    intres=1;  while(k--) res*=N; returnRes;}BOOLIsPrime (intN//Judging prime numbers, because the data is not big, too lazy to print the prime list{     for(intI=2; i*i<=n;i++){        if(n%i==0)return false; }    return true;}intChangeintNintIintJ//the conversion of analog number, there are many techniques, must be careful{    intF=n/mypow (Ten, i+1) *mypow (Ten, i+1);//Reserved Prefixes    intR=n%mypow (Ten, i);//reserved suffix    intMid=j*mypow (Ten, i);//Change the middle number    returnF+mid+R;}BOOLBFs () {memset (Vis,0,sizeof(VIS)); Queue<node>Q; Q.push ({n,0}); Vis[n]=1;  while(!Q.empty ()) {Node now=Q.front ();        Q.pop (); if(now.num==m) {ans=Now.step; return true; }         for(intI=0;i<4; i++) {//from 1th Place to fourth place             for(intj=0; j<=9; j + +) {//change the position number to J                intnextnum=Change (NOW.NUM,I,J); if(nextnum< +|| NEXTNUM&GT;=MAXN)Continue;//border Control                if(vis[nextnum]| |! IsPrime (Nextnum))Continue; Vis[nextnum]=1; Q.push ({nextnum,now.step+1}); }        }    }    return false;}intMain () {intT;cin>>T;  while(t--) {cin>>n>>m; if(BFS ()) cout<<ans<<Endl; Elsecout<<"Impossible"<<Endl; }    return 0;}
Poj3126_bfs

Poj3126--bfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.