BFS+素数打表

这是一道关于寻找两个四位素数之间变换次数最少的路径的问题,每次只能变换一个数字,且变换后的数必须是素数。通过使用广度优先搜索(BFS)策略,配合素数判断函数,寻找最小成本的转化路径。当无法找到满足条件的路径时,输出'Impossible'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

给你两个4位数,要求你每次只能变换一位数字,并且变换前后的数字都要满足是素数;求最少变换次数。

题目大意就是给你两个四位数数a和b然后a每次可以改变a的个位十位百位或者千位上的一个数字,求出最终得到数字b所需要的最短次数,

当然每次改变数字得到的数字需要是一个质数,如果你无论如何都不能在要求每次变换都为质数的约束情况下把a转化为b那么输出Impossible否则求出最短的次数,所以这道题的简单思路就是分别搜索每一次改变的位数之后得到的新数字是不是质数且能不能等于这个目标b的值,

如果得到的是质数则次数+1,直到最终得到想要的结果为止,统计累加的次数,然后就是分析怎么优化了,首先它说的是素数,所以应该先用一个BOOL函数来筛选出满足条件的素数,

在筛选过程中记得将判定素数的那个循环的复杂度变成根号N以免增加电脑的计算量,筛选素数之后再用BFS进行搜索统计就可以了,依然是利用队列来做,当然对于细节的处理上就是要注意每一位的取值情况,个位必然是奇数,千位得从1开始,十位百位嘛改变时没有特殊要求。

 

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int prime[10010];
struct node
{
    int a,step;
}pp,qq;
void init()
{
    memset(prime,0,sizeof(prime));
    int i,j;
    prime[1]=1;
    for(i=2;i<10010;i++)
    {
        if(!prime[i])
        {
            for(j=2;j*i<=10010;j++)
                prime[i*j]=1;
        }
    }
}
int dfs(int kai,int wei)
{
    int vis[10010],i,j;
    memset(vis,0,sizeof(vis));
    int sum;
    queue<node>H;
    pp.a=kai;
    pp.step=0;
    vis[kai]=1;
    H.push(pp);
    while(!H.empty())
    {
        pp=H.front();
        H.pop();
        if(pp.a==wei)
            return pp.step;
        int t[5];
        t[1]=pp.a/1000;
        t[2]=pp.a/100%10;
        t[3]=pp.a/10%10;
        t[4]=pp.a%10;
        for(i=1;i<=4;i++)
        {
            int te=t[i];
            for(j=0;j<10;j++)
            {
                if(t[i]!=j)
                {
                    t[i]=j;
                    sum=t[1]*1000+t[2]*100+t[3]*10+t[4];
                }
                if(sum>=1000&&sum<10000&&!vis[sum]&&!prime[sum])
                {
                    qq.a=sum;
                    qq.step=pp.step+1;
                    H.push(qq);
                    vis[sum]=1;
                }
            }
            t[i]=te;
        }
    }
    return -1;
}
int main()
{
    int N;
    init();
    scanf("%d",&N);
    int n,m;
    while(N--)
    {
        scanf("%d %d",&n,&m);
        int s=dfs(n,m);
        if(s==-1)
            printf("Impossible\n");
        else
            printf("%d\n",s);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值