专栏首页calmoundpoj 3126 Prime Path

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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • uva 699 The Falling Leaves

    Each year, fall in the North Central region is accompanied by the brilliant colo...

    用户1624346
  • UVa Automatic Editing

    uva的题真的很好,每个题都能长许多知识,A了后很开心,这道题我用了两天写,只一道题就学了四个函数,成长不少 Problem E: Automatic Edit...

    用户1624346
  • UVA Hangman Judge

    英语太烂啊。 In ``Hangman Judge,'' you are to write a program that judges a series of ...

    用户1624346
  • Common Pitfalls to Avoid when using HTML5 Application Cache

    Application Cache, also known as AppCache, has been a pretty hot topic with web ...

    IMWeb前端团队
  • JDK7并行计算框架介绍一 Fork/Join概述(官方原版-英文)

    New in the Java SE 7 release, the fork/join framework is an implementation of th...

    数据饕餮
  • 私有双终端假设检验(CS IT)

    我们研究带有简单假设的私有两终端假设测试,其中的隐私目标是确保在告知用户正确的假设时,参与测试协议的用户不会透露有关其他用户观察的其他信息。结果显示,通常,如果...

    蔡秋纯
  • 环境反向散射通信中断性能研究(CS)

    环境反向散射通信(AmBackComs)被认为是物联网的一种频谱和节能技术,因为它允许被动反向散射设备(BDs)将其信息调制成传统信号,例如蜂窝信号,并将它们反...

    蔡秋纯
  • Common Pitfalls to Avoid when using HTML5 Application Cache

    Application Cache, also known as AppCache, has been a pretty hot topic with web ...

    IMWeb前端团队
  • Common Pitfalls to Avoid when using HTML5 Application Cache

    Application Cache, also known as AppCache, has been a pretty hot topic with web ...

    IMWeb前端团队
  • Yet Another Walking Robot (Map)

    There is a robot on a coordinate plane. Initially, the robot is located at the p...

    dejavu1zz

扫码关注云+社区

领取腾讯云代金券