Prime Path POJ - 3126

POJ 3126 Prime Path POJ 3126 Prime Path 题目大意: 给出两个四位数的素数a,b,求从a变到b最少要花几步?每一步只能将a中的一位改变,且改变后的数也要是素数。 具体思路: 埃氏筛+BFS 先求出1000-9999间的素数(可用素数筛,非常快捷)不懂的可看这篇博客筛选素数:埃氏筛选、欧拉筛选(线性筛选)、区间筛选 再用bfs搜索,每次变换一个数字,判断是否为质数,并更新变换次数 注意排除前导为0的情... 阅读详情

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

题意:

给你一个素数beg和一个素数end,让你把beg变成end,每次只能变一位,变化后得到的新的数也还要是素数。求最小变化次数。


分析:

因为要求最小值,所以第一个想到的应该就是bfs,每次变化一位,等到相应的素数,其步数加1。 这里为了防止重复的计算,我们应要求变化之前那个数对应的步数必须小于等于变化之后那个数对应的步数。   为什么呢? 我们记变化之前的数为cur,变化之后的数为next 。  按道理说 step[next] = step[cur] + 1  但是因为程序会做重复的计算,因此我们不能保证当前算出的next是最小的。也就是说有可能之前已经算出了通往next的更少的步数,所以当step[cur] >= step[next] 的时候,这条路我们就可以舍弃了。

注意因为要求最小值,所以我们初始化step数组为无穷大。







#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;

int mypow(int a,int b)
{
    int ans = 1;
    for(int i=0;i<b;i++){
        ans *= a;
    }
    return ans;
}
int change(int temp,int i,int j)
{
    int key = temp/mypow(10,i)%10;
    temp = temp - key*mypow(10,i) + j*mypow(10,i);
    return temp;
}
int beg,en;
bool prime[10005]={0};
int main()
{
    for(int i=2;i<10005;i++){
        if(prime[i] == 0)
        for(int j=i;j*i<10005;j++){
            prime[i*j] = 1;
        }
    }
    
    int T;
    cin >> T;
    int step[10005];                      //store the steps from begin to current.
    while(T--){
        memset(dp,0x3f,sizeof(dp));   // to find the minimum step, we need to initialize the step INF.
        cin >> beg >> en;
        queue<int> q;
        q.push(beg);
        dp[beg] = 0;
        while(!q.empty()){
            int temp = q.front();
            q.pop();
            for(int i=0;i<4;i++){
                for(int j=0;j<10;j++){
                    if(i==3 && j==0) continue;
                    int next = change(temp,i,j);
                    if(prime[next] == 0 && step[next] > step[temp]){        //The second condition avoids plenty of repeated calculations.
                        q.push(next);
                        step[next] = step[temp]+1;
                    }
                }
            }
        }
        cout << step[en] << endl;
    }
}



【回炉重造】POJ-3126.Prime Path 题解 【回炉重造】POJ-1426.Find The Multiple 题解 题目链接:POJ-1426.Find The Multiple 题目 题意 给你一个正整数 n ,让你找一个正整数 m m 必须能被 n 整除 m 是只由 0 和 1 这两种数字组成的 十进制 数 请输出 n 对应的 m 如果有多个结果输出任意一个即可 解题思路 深搜:【广搜要记录的东西太多可能超内存 题上说了不会超过100位,所以我们就定一个超过 100 位的 bitset 位容器 注意: 每一种情况都有判断条件,位置队列 阅读详情

相关推荐

北大POJ3126-Prime Path编程题解析

素数在数学中扮演着重要角色,不仅因为它们是数论的基础,也是现代密码学的关键组成部分。素数的定义相对简单,但其性质却十分复杂,例如哥德巴赫猜想、孪生素数猜想等,都是数论中的未解问题。图由顶点(节点)和边组成,可以是有向的也可以是无向的。在有向图中,边的方向性很重要,而在无向图中边是双向的。图还可以分为有权图和无权图,有权图的边具有权重,表示连接两个节点的成本或距离。例如,社交网络可以看作是一个图,其中顶点代表用户,边代表用户之间的友谊关系。这个图是有向的,因为友谊关系可能是单向的。

weixin_42515340的博客 899

Prime Path(POJ 3126)解题报告

Prime Path(POJ 3126)解题报告 一、原题 Prime Path Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description The ministers of the cabinet were quite upset by the me

Dancinglikelink 973

DDR5 SODIMM标准解读:262 Pin背后的设计与选购指南

DDR5内存时代,笔记本内存条形态从DDR4的260 Pin升级为262 Pin,这背后是供电架构从主板下放到内存条的变革。JEDEC PS-006A-2022标准定义了DDR5 SODIMM的引脚排列、电气参数、机械尺寸等底层规范,是硬件工程师和普通用户理解内存兼容性、性能边界的关键依据。从PMIC电源管理到双通道内部设计,从Speed Bin时序到On-Die ECC,标准不仅解释了为什么DDR5与DDR4物理不兼容,也揭示了内存选购中频率、时序与散热之间的真实关系。本文以该标准为线索,梳理引脚定义、关

weixin_34162228的博客 203

【poj 3126】Prime Path 题意&题解&代码(C++)

poj

曾经追梦的happy时光 889

10xGenomics官方可视化工具——Loupe Browser

Loupe Browser是⼀款10xGenomics开发的交互式桌⾯应⽤程序,⽤于探索10x单细胞数据,可以用来找出感兴趣的细胞、找出显著差异表达的基因、鉴定细胞类型、探索细胞亚类和数据共享。

weixin_47195452的博客 3350

自动驾驶仿真 (三)—— 基于PreScan与Simulink的AEB系统仿真

自动驾驶仿真三—— 基于PreScan&Simulink的AEB系统仿真1. AEB自动紧急制动系统1. 1 TTC碰撞时间模型1. 2 C-NCAP法规部分术语与定义1. 3 主动安全ADAS系统试验项目2. AEB仿真分析建模2.1 搭建场景2.2 传感器建模2.3 添加AEB控制系统3. 典型工况仿真3.1 CCRs仿真测试3.1 CCRm仿真测试4. 参考学习的书目教材 博主前面的博文已经介绍了Prescan&Simulink基本的场景建模及仿真分析流程,《自动驾驶仿真 (二)——

zhudj08的博客 1万+

POJ 3126 Prime Path(素数路径)

POJ3126 Prime Path(素数路径) TimeLimit:1000MSMemoryLimit:65536K Description- 题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stati...

470

SLAM工程师面试避坑指南:2025年最新高频考点与实战代码解析

本文为SLAM工程师提供2025年最新面试避坑指南,涵盖高频技术考点如VINS初始化、三维几何处理、多传感器融合架构设计等,并包含实战代码解析。特别针对算法工程师面试中的常见陷阱,提供系统设计框架和代码模板,帮助求职者提升通过率。

metaverse5vr的博客 245

Poj 3126 Prime path

Poj 3126 Prime path 题意 两个四位数的素数A,B。改变A中的一位数,让它变成素数需要1镑,问由A变成B至少需要多少镑?这样的素数对一共有n组。 思路 这是一道bfs题,一定要记住标记已经访问的数,否则会超时。 代码 #include <cstdio> #include <queue> #include <cmath> struct N...

qq_33005379的博客 276

PID循迹控制算法

前言  让小车进行循迹最重要的是获取车相对于循迹线的位置,关于循迹模块的原理我已经在之前的一篇博客上讲过,如果大家没看过的话可以点此链接:PID循迹模块原理。 PID循迹原理简介 图1 循迹车寻线行走图示 图2 市场常见循迹传感器  PID循迹的原理我们大致可以用常识性思维去思考下,如图1所示为循迹车寻线行走图示,现在小车要按照中间那条黑线循迹行走,如果车头往左偏则需要控制方向盘让车往右转,车头往又偏则需要让车往左转。但是仅仅只有左转和右转的话,车一到左边就向右转,一到右边就向左转,则车会来回在线左右晃动

Dinvent的博客 7万+

SpotBugs使用

spotbugs命令行参数解析

javajingling的专栏 2947

F - Prime Path POJ - 3126(36种情况的进阶BFS)

F - Prime Path POJ - 3126 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 offi...

石旭先森的博客 404

GPL vs MIT:独立开发者选择开源协议的法律与商业考量

你可能听过这样的故事:某个开发者用业余时间写了个超好用的工具库,开源后被某大厂悄悄集成到闭源产品里赚得盆满钵满,开发者却连杯奶茶钱都没拿到;也可能听说过另一个案例:开发者选了GPL协议,结果用户修改代码后必须开源,反而推动项目社区快速壮大,最终靠技术服务赚到了钱。这些故事的核心矛盾,都源于开源协议的选择。对于独立开发者(尤其是单枪匹马写代码的个人或小团队),开源协议不是“随便选一个就行”的小事——它是代码的“使用说明书”,更是保护自身权益、规划项目未来的关键工具。

AI天才研究院 1177

POJ3126 Prime Path

POJ3126 Prime Path 题意:只能通过素数到达想要的那个数,问你最短的路径; 解题思路:拥有所有素数,bfs 复盘过程:虽然是个基本的bfs的题,但想用dfs做,本来想借助dfs搞清楚他的迭代过程,结果搜索的分支十分混乱,不对,应该说迭代的太深了,想想dfs先序遍历的原理就知道不能如此,于是最后向bfs低头。明显基础题。。基础不牢,地动山摇! #include<stdio.h&...

weixin_43818893的博客 218

【ARM AMBA5 CHI 2 -- CHI 链路层详细介绍 】

目前有六个版本的 CHI 协议,CHI-A 是 CHI 协议的第一个版本。它提供了一个传输层,具有减少拥塞的功能。新通道、CHI 术语和组件命名的定义;请求、监听过滤器和缓存状态转换的示例;事务排序、独占访问和分布式虚拟内存(DVM)操作的规则;CHI-B 问题扩展了 CHI-A,但不能直接向后兼容 CHI-A。更大的物理地址宽度;原子事务;DVM 的 VMID 扩展;通道字段、事务结构和可靠性、可用性和可维护性(RAS)特性的描述;

CodingCos的博客 2535

微信小程序:errno: 600001, errMsg: “uploadFile:fail errcode:-101 cronet_error_code:-10001 error_msg:net::E

微信小程序报错uploadFile:fail errcode:-101 cronet_error_code:-10001 error_msg:net::ERR_CONNECTION_RESET, custum ip is invalid, didn't find user's ip,wx.uploadFile errno: 600001 errcode:-101 ?微信小程序wx.uploadFile错误,wx.uploadFile 在真机上报下面的错误,调试工具里没有问题,其它接口在真机/调试工具都正常,

✅ 精选推荐文章 4257

BFS POJ 3126 Prime Path

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

weixin_33717117的博客 137

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

POJ3126 Prime Path

http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 1033173337333739377987798179 分析:用字符串存m,n,这样改变各个位较方便 数组开大一点,开始数组开小了,结果就出错了 #include<stdi...

deyou1988的专栏 179

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

poj3126 Prime Path

package bfs;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;/**问题请参考http://poj.org/problem?id=3126 * @author rayli * @date:2014-7-17 上午9:18:47 * 给定两个四位素数a  b,要求把a变换到b 变换的过程

萝卜@兔子的专栏 337

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Prime Path POJ - 3126

Prime Path POJ - 3126 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. —...

ln2037的博客 288

POj 3126 Prime Path

来源:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11384   Accepted: 6453 Description The minister

MummyDing's Blog 899

POJ 3126 Prime Path DFS

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10352   Accepted: 5913 Description The ministers of the cabinet were quite upset by the message fr

kdwycz的刷题空间 792

POJ - 3126 Prime Path

POJ - 3126 Prime Path 题目 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 ev

jame_19的博客 227
上一篇: vim
下一篇: 离散化
mwhybmj
博客等级 码龄10年 8粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值