POJ3126 Prime Path(bfs)

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

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


给你两个四位数字,每次可以变换一个数字,变换得到的数必须是质数且不能重复,问你最少经过几次可以得到。


枚举每位然后判断是否是素数。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
struct node
{
	/* data */
	int prime, step;
};
int a, b;
bool vis[15000];
node q[15000];
bool judgeprime(int a)
{
	if(a == 2 || a == 3) return true;
	else if(a <= 1 || a % 2 == 0) return false;
	else if(a > 3) {
		for(int i = 3; i * i <= a; ++i)
			if(a % i == 0) return false;
		return true;
	}
}
void bfs()
{
	int head, tail;
	q[head = tail = 0].prime = a;
	q[tail++].step = 0;
	vis[a] = true;
	while(head < tail) {
		node x = q[head++];
		if(x.prime == b) {
			cout << x.step << endl;
			return;
		}
		int unit = x.prime % 10, deca = (x.prime / 10) % 10;
		for(int i = 1; i <= 9; i += 2) {
			int y = (x.prime / 10) * 10 + i;
			if(y != x.prime && !vis[y] && judgeprime(y)) {
				vis[y] = true;
				q[tail].prime = y;
				q[tail++].step = x.step + 1;
			}
		}
		for(int i = 0; i <= 9; ++i) {
			int y = (x.prime / 100) * 100 + i * 10 + unit;
			if(y != x.prime && !vis[y] && judgeprime(y)) {
				vis[y] = true;
				q[tail].prime = y;
				q[tail++].step = x.step + 1;
			}
		}
		for(int i = 0; i <= 9; ++i) {
			int y = (x.prime / 1000) * 1000 + i * 100 + deca * 10 + unit;
			if(y != x.prime && !vis[y] && judgeprime(y)) {
				vis[y] = true;
				q[tail].prime = y;
				q[tail++].step = x.step + 1;
			}
		}
		for(int i = 1; i <= 9; ++i) {
			int y = x.prime % 1000 + i * 1000;
			if(y != x.prime && !vis[y] && judgeprime(y)) {
				vis[y] = true;
				q[tail].prime = y;
				q[tail++].step = x.step + 1;
			}
		}
	}
}
int main(int argc, char const *argv[])
{
	int t;
	cin >> t;
	while(t--) {
		cin >> a >> b;
		memset(vis, false, sizeof(vis));
		bfs();
	}
	return 0;
}


手把手教你用CV5200模块搭建无人机高清图传系统(含实测数据) 本文提供了一份基于CV5200模块搭建远距离无人机高清图传系统的完整实战指南。文章详细解析了系统架构、硬件连接、软件配置与优化,并分享了实地飞行测试数据。核心在于利用CV5200的LR-WiFi私有协议,在视距下实现数公里级的稳定高清视频传输,为DIY爱好者和行业应用提供了高性价比的解决方案。 阅读详情

相关推荐

从理论到实践:利用FilterSolutions高效设计7阶椭圆低通滤波器

本文详细介绍了如何利用FilterSolutions软件高效设计7阶椭圆低通滤波器。文章从理论入手,对比了巴特沃斯、切比雪夫等经典滤波器特性,阐述了在要求高衰减陡度(如62dB抑制)和严格通带波纹(0.2dB)时,椭圆滤波器是最优选择。通过分步实战教程,指导读者完成从软件参数设置、性能验证到实际电路布局与调试的全过程,助力射频与高速数字信号处理工程师将理论转化为可靠设计。

jupi8的博客 559

E - 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 sec...

qq_44134712的博客 499

从Excel到‘一张图’办案:手把手教你用AbutionGraph构建公安经侦知识图谱(含数据建模避坑指南)

本文详细介绍了如何利用AbutionGraph时序图数仓技术,将公安经侦领域的Excel、银行流水等碎片化数据转化为可交互的知识图谱。通过数据清洗、实体关系模型设计、时序数据处理等步骤,实现高效智能分析,提升办案效率。特别包含数据建模避坑指南和实战案例,助力公安金融案件侦破。

weixin_33702545的博客 342

【简单搜索】 POJ 3126 Prime Path详解

Prime Path: 题目链接:https://vjudge.net/problem/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. — It is a matter of sec

Qiuker_jl的博客 1603

ESP-01S获取网络时间踩坑全记录:AT指令调试、JSON解析乱码、时区处理,一个都不能少

本文详细记录了使用ESP-01S WIFI模块获取网络时间的完整过程,包括AT指令调试、JSON解析乱码处理以及时区转换等关键步骤。通过实战案例和代码示例,帮助开发者避开常见陷阱,实现稳定的网络时间同步功能。

weixin_30460489的博客 295

工程师必看:如何用MINITAB快速计算传感器寿命(附详细步骤)

本文为工程师提供了使用MINITAB软件快速计算传感器寿命的详细实战指南。文章重点阐述了如何通过数据预处理、回归分析拟合阿伦尼斯模型,并最终预测平均寿命与置信区间,帮助工程师高效处理加速寿命试验数据,提升可靠性分析工作的准确性与效率。

weixin_29296241的博客 248

E - Prime Path(BFS+埃式筛)

E - 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 every now

Mr.RainsdRop的胡思乱想 1336

别再手动量了!用Python+Open3D给BIM模型做‘CT扫描’,自动揪出施工误差(附完整代码)

本文介绍如何利用Python和Open3D技术实现BIM模型的毫米级施工误差自动检测。通过点云预处理、模型配准和偏差分析,系统能自动生成三维差异热力图和验收报告,显著提升施工质量检测效率。特别适合混凝土结构错位和钢结构安装偏差等常见问题的智能分析。

weixin_29284657的博客 243

搜索练习题I-09

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 s

snayf的博客 409

Python+Tableau 全流程数据分析实战:从数据清洗到商业洞察可视化

本文通过Python+Tableau完整演示电商销售数据分析流程。首先使用Python进行数据清洗(缺失值处理、异常值过滤等)和探索性分析(商品类别、时间趋势等),然后导入Tableau创建交互式可视化仪表盘。分析发现电子产品销售额最高但利润率低,美妆类利润率最优,东部地区销售表现最好。最终提出优化供应链、加强美妆推广等商业建议,展示了Python数据处理与Tableau可视化的高效配合。所有代码可直接运行,适合数据分析初学者和从业者实战参考。

weixin_44520581的博客 337

EtherCAT主站SOEM -- 19 --Qt-Soem通过CSP模式(周期同步位置模式)控制一个电机转圈圈

以上就是EtherCAT主站SOEM – 19 --Qt-Soem通过CSP模式(周期同步位置模式)控制一个电机转圈圈的内容。有不明白的地方欢迎留言;有建议欢迎留言,我后面编写文档好改进。创作不容,如果文档对您有帮助,记得给个赞。

常驻客栈的博客--嵌入式EtherCAT 1996

ayit第十四周训练f题

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

m0_46628834的博客 565

8.26华为OD机试真题 新系统 - 字符串回文判 (Java/Py/C/C++/Js/Go)

8.26华为OD机试真题 新系统 - 字符串回文判 (Java/Py/C/C++/Js/Go)

算法大师! 1235

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...

石旭先森的博客 405

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

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

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

(BFS)POJ3126Prime Path

POJ3126Prime Path 题意&思路: 给两个数,又第一个数改变到第二个数的奇数路径最少是多少。每次只能改变一位,且不含前导0,每次改变后的数都要为奇数。 先把素数筛出来,然后按位子改变bfs即可,第一位不能为0以及最后一位一定不可能是偶数。 代码: #include<iostream> #include<stdio.h> #include<std...

Z7784562的博客 1519

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Prime Path POJ - 3126 bfs

Prime Path POJ - 3126 最近一直在联系kuangbin的题目,发现自己有好多没有掌握的东西。。 题目大义 给定一个两个4位素数,一个是起始素数,一个是终止素数,从起始素数开始改变素数中的一位数字,保证其仍为素数,再从这个改变后的素数开始再改变一位数,仍保证其是素数,知道转换到终止素数,问最小的转换次数是多少? 这种搜索题看到最小转换次数就知道用bfs去做,由于我们要维护转换时的素数性质,所以我们还要得到从1000 到 10000 之间的素数 于是这道题就是素数筛+bfs 由于这里只

qq_35423505的博客 234

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

BFS POJ 3126 Prime Path

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

weixin_33717117的博客 137

POJ 3126 Prime Path bfs

http://poj.org/problem?id=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 ...

xiji333的博客 264

Prime Path POJ - 3126 bfs

一、内容 题意:给定2个质数,代表开头和结尾,每次只能改变一位上的数字,且改变后的数也要是质数,求最少几次能变成结尾的数字。 二、思路 先筛选1000-9999的质数存在is_peime里面。 bfs搜索将每位上的数字进行改变,枚举下所有情况,若改变后的数字是质数且未访问过就入队。 通过整除和取余得到某位上的数字,然后再修改成需要的数。 三、代码 #include <cstdio>...

Tizzi的博客 911

POJ 3126 Prime Path (BFS)

POJ 3126 Prime Path (BFS) 【题目链接】http://poj.org/problem?id=3126 【题目大意】 给你两个四位数的素数,通过改变其中的一个数,每次只允许改变一位数,而且改变之后的数也必须是素数,问最少通过改变几次变成后一个四位的素数。如果不能改变成后面的四位素数则输出Impossible 【题解】 不罚时,每次改变四位中的一位入队; 可以先处理出四位素数,...

ding_ning123的博客 177

POJ 3126 Prime Path(BFS)

题目链接:POJ 3126 Prime Path 题意:给出两个四位素数,要求从第一个素数变到第二个素数。            变换规则:每次只能改变四位数中的一个数,并且要求变换后的数依旧是素数。            求最少的变换次数。 思路:先把1000-10000之间的数判断好是不是素数,之后直接用。            然后BFS搜索,每次变换一个数,变换后的数要求是素数,

Must so 392

poj 3126Prime Path--BFS

题目来源:poj 3126 大致题意: 给定两个四位素数a b,要求把a变换到b 变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。 求从a到b最少需要的变换次数。无法变换则输出Impossible。 #include <iostream> #include <que...

圣托里尼的日落啊~的博客 249

【ACM】【BFS】POJ 3126 Prime Path

Prime Path 点我前往题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32181 Accepted: 17439 Description The ministers of the cabinet were quite upset by the message fro...

lesileqin的博客 319

poj 3126 Prime Path bfs

题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit:1000MS Memory Limit:65536K Total Submissions:32036 Accepted:17373 Description The ministers of the cab...

weixin_30641465的博客 104

Prime Path POJ - 3126(BFS)

Prime Path POJ - 3126 https://vjudge.net/problem/POJ-3126 题意: 给一个四位数n,每次可以改变一位数,而且改变后的这个数必须是四位的素数,求最少改变几次可以变到m。 Sample Input 3 1033 8179 1373 8017 1033 1033 Sample Output 6 7 0 样例一: 1033 1733 3733 ...

NaNa 250

POJ 3126 Prime Path BFS

题意:给你2个素数,a,b,问你a经过多少次变换才能变成b. 一次变换只能改变a的一个位,并且改变后的a必须是素数。 思路:简单bfs。注意每次改变的a。 #include #include #include #include #include #include #include #include #include #include #include #includ

just_water 964
上一篇: Codeforces Round #315 (Div. 2)569D Symmetric and Transitive(dp)
下一篇: POJ2251 Dungeon Master(bfs)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值