首页 > 代码库 > Prime Path (poj 3126 bfs)

Prime Path (poj 3126 bfs)

Language:
Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11703 Accepted: 6640

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

Source

Northwestern Europe 2006

题意:  给定两个素数(四位数),求第一个数经过几次转换能够得到第二个素数。转换方式:是变换数中某一位的数字(第一位不能为零,其他的变换数字是0~~9),变换之后的数也为素数。

思路:bfs,搜索求最短路径,很容易就想到广度优先搜索;因为广度优先搜索,第一次搜到得到的步数就是最少的步数。另外打素数表提高判断的时候的效率

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

bool ISprime[maxn];
bool visit[maxn];
int m,n,a,b,c,d;

struct Node
{
    int p[4];//用数组存各位数
    int step;
};

void prime()  //素数筛法
{
    for (int i=2;i<maxn;i++)
    {
        if (i%2)
            ISprime[i]=true;
        else
            ISprime[i]=false;
    }
    int m=sqrt(10010.0);
    for (int i=3;i<m;i++)
    {
        if (ISprime[i])
        {
            for (int j=i+i;j<maxn;j+=i)
                ISprime[j]=false;
        }
    }
}

int bfs()
{
    Node st,now;
    memset(visit,false,sizeof(visit));
    queue<Node>Q;
    while (!Q.empty())
        Q.pop();
    visit[m]=true;
    st.p[0]=m/1000;st.p[1]=(m/100)%10;st.p[2]=(m/10)%10;st.p[3]=m%10;
//    printf("%d %d %d %d\n",st.a[0],st.a[1],st.a[2],st.a[3]);
    st.step=0;
    Q.push(st);
    while (!Q.empty())
    {
        st=Q.front();
        Q.pop();
        if (st.p[0]==a&& st.p[1]==b&&st.p[2]==c&&st.p[3]==d)
        {
            return st.step;
        }
        for (int i=0;i<=3;i++)
        {
            for (int j=0;j<10;j++)
            {
                if (st.p[i]==j)
                    continue;
                if (i==0&&j==0)
                    continue;
                now.p[0]=st.p[0];
                now.p[1]=st.p[1];
                now.p[2]=st.p[2];
                now.p[3]=st.p[3];
                now.p[i]=j;
                int x=now.p[0]*1000+now.p[1]*100+now.p[2]*10+now.p[3];
                if (ISprime[x]&&!visit[x])
                {
                    visit[x]=true;
                    now.step=st.step+1;
                    Q.push(now);
                }
            }
        }
    }
    return -1;
}

int main()
{
    prime();
    int cas;
    scanf("%d",&cas);
    while (cas--)
    {
        scanf("%d%d",&m,&n);
        a=n/1000;b=(n/100)%10;c=(n/10)%10;d=n%10;
//        printf("%d %d %d %d\n",a,b,c,d);
        int ans=bfs();
        if (ans==-1)
            printf("Impossible\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
/*
3
1033 8179
1373 8017
1033 1033
*/



Prime Path (poj 3126 bfs)