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 条评论
登录 后参与评论

相关文章

来自专栏desperate633

LeetCode Fizz Buzz题目分析代码

Write a program that outputs the string representation of numbers from 1 to n.

831
来自专栏猿人谷

求大于整数m且紧靠m的k个素数 及 判断一个数是否为素数的方法

题目:   请编写一个函数void fun(int m,int k ,int xx[]),该函数的功能是:将大于整数m且紧靠m的k个素数存入xx所指的数组中。 ...

1869
来自专栏菩提树下的杨过

无限级分类(非递归算法/存储过程版/GUID主键)完整数据库示例_(2)插入记录

-- ======================================== -- Author:  <杨俊明,jimmy.yang@cntvs.c...

2029
来自专栏葡萄城控件技术团队

深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类、类成员函数、类成员变量进行访问控制。同时,访问控制符也是语法保留关键字,用于封装组件。 Pub...

3299
来自专栏码匠的流水账

聊聊flink的SourceFunction

flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

2643
来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

1152
来自专栏软件开发 -- 分享 互助 成长

CRC校验码

循环冗余校验码(CRC)的基本原理是:在K位信息码后再拼接R位的校验码,整个编码长度为N位,因此,这种编码也叫(N,K)码。对于一个给定的(N,K)码,可以证明...

2856
来自专栏前端说吧

正则表达式验证汇总

一、收集1  (转自https://blog.csdn.net/jumtre/article/details/13775351)

1261
来自专栏码匠的流水账

springboot2自定义statsd指标前缀

springboot2引入了micrometer,1.x版本的spring.metrics.export.statsd.prefix在2版本中已经被标记为废弃,...

1462
来自专栏个人分享

XML封装与验证消息

2184

扫码关注云+社区

领取腾讯云代金券