[POJ]P3126

Prime Path

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35230   Accepted: 18966

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

Source


两年没写了,现在已经真的菜爆了。
本题大致意思,给你两个四位的素数,一个是起始状态,另一个是终止状态,要使起始状态变为终止状态每一步可进行的操作为,将这个四位数的某一位更换,但要求新的数也必须是一个素数,问最少步数。
大致思路也比较简单,就是先欧拉线性筛把所有素数先筛出来,再用在线处理的方式bfs。(然而我bfs写炸了好几次...)
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
 ;
 ;
namespace iNx{
    struct qq{
        int num;
        int c;
    };
    qq q[Maxn];
    int primer[Maxn],pos[Maxn];
    bool check[Maxn],exist[Maxn];
    int cnt,best;
    void Euler(){
        memset(check,,sizeof check);
        int i,j;
        check[]=false ;
        ;i<;i++){
            if(check[i]) primer[++cnt]=i;
            ;j<=cnt&&(i*primer[j]<);j++){
                check[i*primer[j]]=false ;
                ) break ;
            }
        }
    }
    int getn(int a,int i){
        ) ;
        ) )%;
        ) )%;
        ;
    }
    void bfs(int a,int b){
        ,tail=;
        q[].num=a;
        q[].c=;
        int i,j,h,k,m,t;
        while(tail>=head){
            h=q[head].num;
            if(h==b){
                printf("%d\n",q[head].c);
                break ;
            }
            exist[h]=true ;
            ;i<=;i++){
                k=getn(h,i);
                ,m=;j<i;j++) m*=;
                ;j<=;j++){
                    &&j==) continue ;
                    if(j==k) continue ;
                    t=h+j*m-k*m;
                    if(check[t]&&(!exist[t])){
                        q[++tail].num=t;
                        q[tail].c=q[head].c+;
                    }
                }
            }
            head++;
        }
    }
    int main(){
        Euler();
        int n,a,b,i;
        scanf("%d",&n);
        ;i<=n;i++){
            scanf("%d%d",&a,&b);
            memset(exist,,sizeof exist);
            bfs(a,b);
        }
        ;
    }
}
int main(){
    iNx::main();
    ;
}

现在要开始天天练习了,蒟蒻我太难了。

[POJ]P3126 Prime Path[BFS]的更多相关文章

  1. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  3. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  4. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  5. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  6. POJ 3126 Prime Path BFS搜索

    题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...

  7. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

  8. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  9. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

随机推荐

  1. 写出java.lang.Object类的六个常用方法

    java是面向对象的语言,而Object类是java中所有类的顶级父类(根类). 每个类都使用Object类作为超类,所有对象(包括数组)都实现这个类的方法,即使一个类没有用extends明确指出继承 ...

  2. Sqlserver限制用户访问指定数据库

    USE master CREATE LOGIN test --要创建的用户名 WITH PASSWORD = '123456', --密码 DEFAULT_DATABASE = DBTest, --指 ...

  3. AKKA学习(二) 未完

    Actor调用 从上面的例子中,我们可以大概的对AKKA在JAVA中的使用有一个全局的概念.这里我们在稍微细致的讲解一下. 在JAVA中使用AKKA进行开发主要有这几个步骤: 定义消息模型. 创建Ac ...

  4. Centos7 升级php版本到php7

    一.首先查看是否有老版本 yum list installed | grep php 二.如果安装的有 yum remove php.x86_64 php-cli.x86_64 php-common. ...

  5. zookeeper 选举leader详解

    一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...

  6. 2019牛客暑期多校训练营(第九场) - B - Quadratic equation - 二次剩余

    https://ac.nowcoder.com/acm/contest/889/B 假如我们能够求出 \(x-y\) 在模p意义的值,那么就可以和 \(x+y\) 联立解出来了. 由于 \((x-y) ...

  7. [转载]Linux软件包及dpkg\apt等方法

    Linux软件安装 来源:https://segmentfault.com/a/1190000011200004?share_user=1030000007255638 一.安装包分类 在Linux平 ...

  8. scala 从头越

    一个综合小例子, 要严格区分 函数与方法 , 与 java 不一样 /** * Scala 的值类型有 7 种 * Byte * Char * Short * Int * Long * Float * ...

  9. python 单引号、双引号和三引号混用

    单引号: 当单引号中存在单引号时,内部的单引号需要使用转义字符,要不然就会报错: 当单引号中存在双引号时,双引号可以不用加转义字符,默认双引号为普通的字符,反之亦然. 双引号: 当双引号中存在双引号时 ...

  10. Uber回馈开源的一些软件

    AresDB AresDB 是 Uber 开源的一个基于 GPU 运算的实时分析存储引擎和查询引擎.具备低查询延迟.高数据刷新率和高效内存和磁盘存储管理.AresDB 要求 CUDA Toolkit ...