[kuangbin带你飞]专题一 简单搜索-F - Prime Path

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15921 Accepted: 8987

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


分析:一个数字枚举出所有的状态,就是bfs嘛。水题,双向bfs如何实现是个问题。


code:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>

using namespace std;
typedef long long ll;
typedef struct{
    int number[4];
    int wal;
}data;
data star,en;
int n,m;
int is_prim[10005],vis[10005];
int dir[]={1,10,100,1000};

void init()
{
    cin>>n>>m;
    memset(vis,0,sizeof vis);
    star.number[0]=n/1000;
    star.number[1]=n/100%10;
    star.number[2]=n/10%10;
    star.number[3]=n%10;
    en.number[0]=m/1000;
    en.number[1]=m/100%10;
    en.number[2]=m/10%10;
    en.number[3]=m%10;
    star.wal=0;
    en.wal=0;
}

void prim_()
{
    memset(is_prim,0,sizeof is_prim);
    for(int i=2;i<=9999;i++)
    {
        if(is_prim[i]==1)
            continue;
        for(int j=i*2;j<=9999;j+=i)
        {
            is_prim[j]=1;
        }
    }

}

void bfs()
{
    queue <data> qf;
    queue <data> qb;
    vis[n]=1;

    qf.push(star);
    qb.push(en);

    while(qf.size())
    {
        data nos,noe;
        nos=qf.front();
        noe=qb.front();
        int stemp=0,etemp=0;
        for(int i=0;i<4;i++)
        {
            stemp=stemp*10+nos.number[i];
            etemp=etemp*10+noe.number[i];
        }
        qf.pop();
        qb.pop();
        if(stemp==m)
        {
            cout<<nos.wal<<endl;
            return ;
        }
       //r printf("%d %d\n",stemp,etemp);

        data ne1,ne2;
        ne1=nos,ne2=noe;
        ne1.wal=nos.wal+1;
        ne2.wal=noe.wal+1;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<=9;j++)
            {
                ne1=nos;
                ne2=noe;
                if(i==0&&j==0) continue;
                ne1.number[i]=j;
                ne2.number[i]=j;
                stemp=0,etemp=0;
                for(int q=0;q<4;q++)
                {
                    stemp=stemp*10+ne1.number[q];
                    etemp=etemp*10+ne2.number[q];
                }

                if(1000<=stemp&&etemp<=9999)
                {
                    if(!vis[stemp]&&!is_prim[stemp])
                    {
                        vis[stemp]=1;
                        ne1.wal=nos.wal+1;
                        qf.push(ne1);
                    }
                }
                if(1000<=etemp&&etemp<=9999)
                {
                    if(!vis[etemp]&&is_prim[etemp])
                    {
                        vis[etemp]=1;
                        ne2.wal=noe.wal+1;
                        qb.push(ne2);
                    }
                }
            }
        }
    }
}


int main(void)
{
    prim_();
    int t;
    cin>>t;
    while(t--)
    {
        init();
        bfs();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值