POJ3126 Prime Path

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

题意:输入两个素数,求从A转到B的最短路径,每次只能转换一个数字..
思路对素数打表,然后BFS求最短路径“

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 10000
bool prime[MAX];

void init()                //素数打表
{
    int i,j;
    for(i=1000;i<MAX;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            {
                prime[i]=false;
                break;
            }
        }
        if(j==i)prime[i]=true;
    }
}

int bfs(int first,int last)
{
    bool dis[MAX];
    queue<int>q;
    int v,j,i,temp,vtemp,count1[MAX],t[4];
    memset(dis,false,sizeof(dis));
    memset(count1,0,sizeof(count1));

    q.push(first);
    dis[first]=true;

    while(!q.empty())
    {
        v=q.front();
        //cout << v << "-";
        q.pop();

        t[0]=v/1000;
        t[1]=v%1000/100;
        t[2]=v%100/10;
        t[3]=v%10;

        for(j=0;j<4;j++)
        {
           temp=t[j];
           for(i=0;i<10;i++)
           {
               if(i!=temp)
                {
                    t[j]=i;
                   vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];
                  // cout<<"vtemp:"<<vtemp<<endl;
                   if(!dis[vtemp]&&prime[vtemp])              //检测所遍历的数是否是素数,dis数组记录vtemp是否被遍历过
                   {
                  //     cout<<"素数vtemp:"<<vtemp<<endl;
                       count1[vtemp]=count1[v]+1;
                       dis[vtemp]=true;
                       q.push(vtemp);                          //素数入队
                   }
                   if(vtemp==last) return count1[vtemp];
                }
           }
             t[j]=temp;               //将t[j]还原
        }
        if(v==last)
            return count1[v];
    }
    return -1;
}

int main()
{
    int st,ed,n;
    int ans;
    init();
    //for (int i = 1000; i < 10000; i++)
    //  if (prime[i])
    //      cout << i << "--" ;
    cin>>n;
    while(n--)
    {
        cin>>st>>ed;
        ans=bfs(st,ed);
        if(ans!=-1)cout<<ans<<endl;
        else cout<<"Impossible"<<endl;
    }
    return 0;
}

打表的数据:

1009--1013--1019--1021--1031--1033--1039--1049--1051--1061--1063--1069--1087--10
91--1093--1097--1103--1109--1117--1123--1129--1151--1153--1163--1171--1181--1187
--1193--1201--1213--1217--1223--1229--1231--1237--1249--1259--1277--1279--1283--
1289--1291--1297--1301--1303--1307--1319--1321--1327--1361--1367--1373--1381--13
99--1409--1423--1427--1429--1433--1439--1447--1451--1453--1459--1471--1481--1483
--1487--1489--1493--1499--1511--1523--1531--1543--1549--1553--1559--1567--1571--
1579--1583--1597--1601--1607--1609--1613--1619--1621--1627--1637--1657--1663--16
67--1669--1693--1697--1699--1709--1721--1723--1733--1741--1747--1753--1759--1777
--1783--1787--1789--1801--1811--1823--1831--1847--1861--1867--1871--1873--1877--
1879--1889--1901--1907--1913--1931--1933--1949--1951--1973--1979--1987--1993--19
97--1999--2003--2011--2017--2027--2029--2039--2053--2063--2069--2081--2083--2087
--2089--2099--2111--2113--2129--2131--2137--2141--2143--2153--2161--2179--2203--
2207--2213--2221--2237--2239--2243--2251--2267--2269--2273--2281--2287--2293--22
97--2309--2311--2333--2339--2341--2347--2351--2357--2371--2377--2381--2383--2389
--2393--2399--2411--2417--2423--2437--2441--2447--2459--2467--2473--2477--2503--
2521--2531--2539--2543--2549--2551--2557--2579--2591--2593--2609--2617--2621--26
33--2647--2657--2659--2663--2671--2677--2683--2687--2689--2693--2699--2707--2711
--2713--2719--2729--2731--2741--2749--2753--2767--2777--2789--2791--2797--2801--
2803--2819--2833--2837--2843--2851--2857--2861--2879--2887--2897--2903--2909--29
17--2927--2939--2953--2957--2963--2969--2971--2999--3001--3011--3019--3023--3037
--3041--3049--3061--3067--3079--3083--3089--3109--3119--3121--3137--3163--3167--
3169--3181--3187--3191--3203--3209--3217--3221--3229--3251--3253--3257--3259--32
71--3299--3301--3307--3313--3319--3323--3329--3331--3343--3347--3359--3361--3371
--3373--3389--3391--3407--3413--3433--3449--3457--3461--3463--3467--3469--3491--
3499--3511--3517--3527--3529--3533--3539--3541--3547--3557--3559--3571--3581--35
83--3593--3607--3613--3617--3623--3631--3637--3643--3659--3671--3673--3677--3691
--3697--3701--3709--3719--3727--3733--3739--3761--3767--3769--3779--3793--3797--
3803--3821--3823--3833--3847--3851--3853--3863--3877--3881--3889--3907--3911--39
17--3919--3923--3929--3931--3943--3947--3967--3989--4001--4003--4007--4013--4019
--4021--4027--4049--4051--4057--4073--4079--4091--4093--4099--4111--4127--4129--
4133--4139--4153--4157--4159--4177--4201--4211--4217--4219--4229--4231--4241--42
43--4253--4259--4261--4271--4273--4283--4289--4297--4327--4337--4339--4349--4357
--4363--4373--4391--4397--4409--4421--4423--4441--4447--4451--4457--4463--4481--
4483--4493--4507--4513--4517--4519--4523--4547--4549--4561--4567--4583--4591--45
97--4603--4621--4637--4639--4643--4649--4651--4657--4663--4673--4679--4691--4703
--4721--4723--4729--4733--4751--4759--4783--4787--4789--4793--4799--4801--4813--
4817--4831--4861--4871--4877--4889--4903--4909--4919--4931--4933--4937--4943--49
51--4957--4967--4969--4973--4987--4993--4999--5003--5009--5011--5021--5023--5039
--5051--5059--5077--5081--5087--5099--5101--5107--5113--5119--5147--5153--5167--
5171--5179--5189--5197--5209--5227--5231--5233--5237--5261--5273--5279--5281--52
97--5303--5309--5323--5333--5347--5351--5381--5387--5393--5399--5407--5413--5417
--5419--5431--5437--5441--5443--5449--5471--5477--5479--5483--5501--5503--5507--
5519--5521--5527--5531--5557--5563--5569--5573--5581--5591--5623--5639--5641--56
47--5651--5653--5657--5659--5669--5683--5689--5693--5701--5711--5717--5737--5741
--5743--5749--5779--5783--5791--5801--5807--5813--5821--5827--5839--5843--5849--
5851--5857--5861--5867--5869--5879--5881--5897--5903--5923--5927--5939--5953--59
81--5987--6007--6011--6029--6037--6043--6047--6053--6067--6073--6079--6089--6091
--6101--6113--6121--6131--6133--6143--6151--6163--6173--6197--6199--6203--6211--
6217--6221--6229--6247--6257--6263--6269--6271--6277--6287--6299--6301--6311--63
17--6323--6329--6337--6343--6353--6359--6361--6367--6373--6379--6389--6397--6421
--6427--6449--6451--6469--6473--6481--6491--6521--6529--6547--6551--6553--6563--
6569--6571--6577--6581--6599--6607--6619--6637--6653--6659--6661--6673--6679--66
89--6691--6701--6703--6709--6719--6733--6737--6761--6763--6779--6781--6791--6793
--6803--6823--6827--6829--6833--6841--6857--6863--6869--6871--6883--6899--6907--
6911--6917--6947--6949--6959--6961--6967--6971--6977--6983--6991--6997--7001--70
13--7019--7027--7039--7043--7057--7069--7079--7103--7109--7121--7127--7129--7151
--7159--7177--7187--7193--7207--7211--7213--7219--7229--7237--7243--7247--7253--
7283--7297--7307--7309--7321--7331--7333--7349--7351--7369--7393--7411--7417--74
33--7451--7457--7459--7477--7481--7487--7489--7499--7507--7517--7523--7529--7537
--7541--7547--7549--7559--7561--7573--7577--7583--7589--7591--7603--7607--7621--
7639--7643--7649--7669--7673--7681--7687--7691--7699--7703--7717--7723--7727--77
41--7753--7757--7759--7789--7793--7817--7823--7829--7841--7853--7867--7873--7877
--7879--7883--7901--7907--7919--7927--7933--7937--7949--7951--7963--7993--8009--
8011--8017--8039--8053--8059--8069--8081--8087--8089--8093--8101--8111--8117--81
23--8147--8161--8167--8171--8179--8191--8209--8219--8221--8231--8233--8237--8243
--8263--8269--8273--8287--8291--8293--8297--8311--8317--8329--8353--8363--8369--
8377--8387--8389--8419--8423--8429--8431--8443--8447--8461--8467--8501--8513--85
21--8527--8537--8539--8543--8563--8573--8581--8597--8599--8609--8623--8627--8629
--8641--8647--8663--8669--8677--8681--8689--8693--8699--8707--8713--8719--8731--
8737--8741--8747--8753--8761--8779--8783--8803--8807--8819--8821--8831--8837--88
39--8849--8861--8863--8867--8887--8893--8923--8929--8933--8941--8951--8963--8969
--8971--8999--9001--9007--9011--9013--9029--9041--9043--9049--9059--9067--9091--
9103--9109--9127--9133--9137--9151--9157--9161--9173--9181--9187--9199--9203--92
09--9221--9227--9239--9241--9257--9277--9281--9283--9293--9311--9319--9323--9337
--9341--9343--9349--9371--9377--9391--9397--9403--9413--9419--9421--9431--9433--
9437--9439--9461--9463--9467--9473--9479--9491--9497--9511--9521--9533--9539--95
47--9551--9587--9601--9613--9619--9623--9629--9631--9643--9649--9661--9677--9679
--9689--9697--9719--9721--9733--9739--9743--9749--9767--9769--9781--9787--9791--
9803--9811--9817--9829--9833--9839--9851--9857--9859--9871--9883--9887--9901--99
07--9923--9929--9931--9941--9949--9967--9973--

然后依次入队的数据,即 v

1439-3733-4733-6733-9733-1723-1753-1783-4933-7933-8933-1913-1973-1993-1931-4013-
7013-9013-1213-1613-1019-2063-1163-1663-1061-1069-4093-8093-1193-1693-1091-1097-
1231-1531-1831-1021-1051-2039-5039-7039-8039-1009-1049-3533-3833-3413-3463-7333-
9133-9533-9833-9403-9413-9463-9473-9431-9437-9439-2423-4423-8423-1123-1223-1523-
1823-1427-1429-1153-1553-1451-1459-4483-5483-1283-1583-1481-1487-1489-4493-1499-
1409-3793-3739-4133-4703-4723-4783-4793-6133-6833-6703-6763-6793-6737-9743-9739-
7723-1721-2753-7753-8753-1759-5783-8783-1787-1789-4903-4943-4973-4993-4931-4937-
7963-7993-7937-8233-8923-8963-9973-1373-1873-1979-1997-1999-3931-9931-1901-1951-
4513-4813-4003-4073-4019-7213-7043-7019-9613-9043-9011-2213-1217-3613-1619-3019-
1319-2663-2963-2003-2053-2083-2069-3163-6163-1103-4663-8663-1667-1669-3061-1361-
1861-7069-8069-4091-4099-8293-8693-8893-8053-7193-2693-5693-1697-1699-6091-9091-
1291-1297-1597-1087-4231-5231-8231-1201-1237-2531-5531-1511-1571-4831-8831-1801-
1811-1871-4021-5021-1321-1621-4051-5051-1151-2239-2339-2539-2939-2029-2089-2099-
5639-5839-5939-5009-5059-5099-7639-7079-8539-8839-8009-8059-8089-1109-1609-1709-
3049-4049-9049-1249-1549-1949-3583-3593-3539-2833-3803-3823-3853-3863-5413-3313-
4463-3461-3467-3469-2333-5333-7393-7331-9103-9173-9137-9539-9803-9883-9839-9203-
9419-9461-9467-2473-6473-9479-5431-8431-9631-9421-9491-2437-5437-9337-9497-9239-
4523-4421-8123-8623-8443-8429-1129-1229-7523-1543-6823-7823-6427-1327-1627-1447-
2153-4153-5153-6553-1559-4451-6451-7451-1471-2459-7459-1259-4283-4583-4481-5683-
5443-7283-9283-1289-7583-6481-7481-1181-1381-7487-1187-1987-7489-1889-3499-7499-
1399-4409-7793-3797-3709-3719-3769-3779-4139-7703-4603-4721-4729-4787-4789-4799-
6113-6143-6173-6131-6803-6863-6883-6203-6701-6709-6263-6563-6761-6791-5737-8737-
6037-6337-6637-5743-9343-9643-9749-9719-9769-7727-9721-1741-2953-2713-7253-7853-
7757-7759-8353-8713-4759-9787-1747-1777-2789-7789-2903-5903-4909-3943-4243-4643-
4273-4373-4673-4999-4951-4337-4637-4957-4967-4987-7237-7537-7907-7927-5233-8243-
8263-8273-8237-3923-5923-9923-8929-8363-8563-8863-8969-3373-6373-1303-7873-1877-
1879-1279-1579-6997-1907-2999-8999-3331-3631-3911-9901-9941-7901-1301-1601-7951-
8951-8513-4517-4519-5813-4817-5003-4001-4007-6073-4079-4219-4919-7243-7211-7219-
6043-7643-7919-9623-9619-9041-2011-3011-5011-6011-8011-9311-9511-9811-9001-2113-
2203-2243-2273-2293-3217-4217-6217-1117-1277-3623-3643-3673-3617-6619-3119-3319-
3919-3079-3089-9319-2633-2683-2969-2503-2803-6053-3083-2383-2081-2087-2269-3167-
3169-7103-4363-8669-1367-1567-1867-1607-1637-1657-5669-7669-3361-3761-3001-3041-
3067-6361-2861-4861-5861-8861-7369-8269-8369-4391-4591-4691-9293-8291-8297-8699-
8803-2393-2593-2699-5393-5623-5653-3697-9697-7699-6491-6691-6991-9391-9791-2297-
4297-5297-7297-4597-8597-5087-8087-4201-4211-4241-4261-4271-5261-5281-5237-8731-
8221-2237-2131-2731-2521-2551-2591-5501-5521-5581-5591-3511-3571-6571-1171-4801-
4871-8821-8837-2801-5801-6871-9871-4621-4027-5821-5081-5023-7321-2621-7621-4651-
4751-4057-5351-5651-5851-6151-7151-9151-4339-2309-2389-2399-2549-2579-2909-6029-
9029-2129-2729-2027-6089-2689-4639-5659-5689-5849-5869-5879-5209-5309-9059-5399-
7649-6079-7879-8599-8537-8819-8849-8209-8609-8389-8689-8081-3109-7109-9109-2609-
3449-4349-4549-4649-9349-9649-9949-7549-6949-7949-3581-3529-3559-2843-2837-3203-
3023-3323-3821-3253-3851-5113-5417-5419-3343-8461-3491-2467-8467-3767-3967-3407-
3457-6469-5303-5323-7351-2137-3137-9127-9157-9187-7883-9887-9829-9859-9209-8419-
9161-9661-9067-9767-9967-2477-6673-5479-9679-5441-5471-9601-6421-9221-9521-2417-
2447-5407-5477-9377-9397-4441-8627-8629-8543-8447-8329-4129-7129-3229-4229-6229-
7229-7573-7529-2543-6323-6827-6829-7829-4327-1307-4447-1847-2143-4253-4157-4159-
5953-6353-6653-6551-7559-4457-7411-7457-2659-7159-3259-4259-4289-5843-5449-9281-
7589-6581-6781-7681-3181-9181-2381-5381-7187-7687-7417-7477-3187-5987-3889-4889-
3299-2797-3727-3209-3701-2719-6719-8719-5779-6779-8779-6


构建树模型:
     第一层   第二层  ..............
1033 1433 -> 3433


             7433


             9433


      1733-> 3733


             4733

             6733

             9733


     1933
     1013

     1063


     1093

     1031    

     1039


**1033-1433-1733-1933-1013-1063-1093-1031-1039-**
**1433**->3433-7433-9433-1423-1453-1483-1493-1439
**1733**->3733-4733-6733-9733-1723-1753-1783-
**1933**->4933-7933-8933-1913-1973-1993-1931-
**1013**->4013-7013-9013-1213-1613-1019-
**1063**->2063-1163-1663-1061-1069-
**1093**->4093-8093-1193-1693-
**1031**-1091-1097-

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/79809211