poj 3126 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

1A~~爽

题意:就是给你两个素数只有四位的a,b,问a如何转换能到达b,转换规则是只能转换一个数字的一位,且每次转换后该数依旧是素数

思路:将1000-10000的素数打表,然后BFS,一个一个的枚举。

复制代码
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=10000;
int vis1[MAXN],vis2[MAXN];

void init()
{
    int i,j;
    memset(vis1,0,sizeof(vis1));
    for(i=2;i<MAXN;i++)
    {
        if(!vis1[i])
        {
            for(j=i*2;j<MAXN;j+=i)
              vis1[j]=1;
        }
    }
}

int BFS(int a,int b)
{
    int step[MAXN];
    int s,head,next;
    memset(step,0,sizeof(step));
    memset(vis2,0,sizeof(vis2));
    queue<int>Q;
    Q.push(a);
    vis2[a]=1;
    while(!Q.empty())
    {
        head=Q.front();
        Q.pop();
        if(head==b) return step[head];
        for(int i=1;i<=4;i++)
        {
            if(i==1) s=1;
            else s=0;
            for(int j=s;j<=9;j++)
            {
                if(i==1) next=j*1000+head%1000;
                else if(i==2) next=head/1000*1000+j*100+head%100;
                else if(i==3) next=head/100*100+j*10+head%10;
                else next=head/10*10+j;
                if(vis1[next]==1) continue;
                if(!vis2[next])
                {
                    step[next]=step[head]+1;
                    if(next==b) return step[next];
                    vis2[next]=1;
                    Q.push(next);
                }
            }
        }
    }
    return -1;
}

int main()
{
    int T,a,b;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&a,&b);
        int ans=BFS(a,b);
        if(ans>=0) printf("%d\n",ans);
        else printf("Impossible\n");
    }
    return 0;
}
复制代码
posted @   calmound  阅读(288)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示