kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126

[kuangbin]专题一 简单搜索 Prime Path POJ - 3126【BFS】 【题目描述】 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... 阅读详情

Prime Path POJ - 3126

题目链接:https://vjudge.net/problem/POJ-3126

题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次数,否则输出“Impossible”.(N,M必须一直是素数)

思路:bfs。四位数,每一位可以替换为0~9,那么我们可以每次改变N中的一位数,然后放入队列中,当然,在替换数字时难免会出现重复的四位数,这样会造成TLE,那么我们可以创建一个bool数组标记出现过的,我们也需要素数筛999 ~ 10000之间的素数(你想删哪里到哪就哪里,不要纠结),因为是bfs,所以第一次出现的新的四位素数一定是替换次数最少的,那么题目就简单了。

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

#define inf (1LL << 31) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)

const int N = (int)1e4 + 10;
bool vis[N]; //素数表 0为素数
bool app[N]; //标记是否出现过
int ans;

struct node{

	int a[4];
	int cost;

	node(int* a,int e){
		rep(i, 0, 3){
			this->a[i] = a[i];
		}
		this->cost = e;
	}

	int x(){ //返回四位数的成员函数
		int num = 0;
		rep(i, 0, 3) num = num * 10 + a[i];
		return num;
	}
};

void get_Prime(){ //素数打表

	rep(i, 2, (int)sqrt(N*1.0)){
		if (!vis[i]){
			for (int p = i * i; p <= N; p += i) vis[p] = true;
		}
	}
}

bool work(int x[], int y){  //true为有答案,false为没答案

	queue<node> que;
	node t (x,0);

	app[t.x()] = true;

	que.push(t);

	if (t.x() == y){
		ans = 0;
		return true;
	}

	while (!que.empty()){

		node tmp = que.front();
		que.pop();

		rep(i, 0, 3){ //1~4不同位置
			rep(j, 0, 9){ //替换为0~9
				if (i == 0 && j == 0) continue; //第一位不能是0
				int tt = tmp.a[i]; //暂存该数
				tmp.a[i] = j;      //改变
				
				//该四位数没有出现过且该数是素数
				if (!app[tmp.x()] && !vis[tmp.x()]){

					app[tmp.x()] = true; //标记一下

					if (tmp.x() == y){ //如果变成了想变成的数了
						ans = tmp.cost + 1;
						return true;
					}
					que.push(node{tmp.a,tmp.cost + 1}); //新的四位数放入队列,花费加一
				}
				tmp.a[i] = tt; //变回原来的四位数
			}
		}

	}

	return false;
}

int main(){

	ios::sync_with_stdio(false);
	cin.tie(0);

	get_Prime();//得到素数表
	int n;
	cin >> n;

	int a, b;
	while (n--){

		memset(app, 0, sizeof(app)); //每次初始化

		cin >> a >> b;

		int aa[4];
		int len = 0;
		rep(i, 0, 3){
			aa[3-len++] = a % 10;
			a /= 10;
		} //分割a变为四个数

		//node tmp(aa, 0);
		//cout << "tmp:::" << tmp.x() << endl;

		if (work(aa, b)) cout << ans << endl;
		else cout << "Impossible" << endl;
	}
	
	return 0;
}

POJ - 3126 Prime Path (kuangbin - 简单搜索) 题目描述(已转换成中文)   给你两个四位的素数a,b。   a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变。   请你计算a最少经过多少次上述变换才能变成b。   例如:1033 -> 8179   1033   1733   3733   3739   3779   8779   8179   最少变换了6次。 输入格式   第一行输入整数T,表示样例数。 (T <= 100)   每个样例输入两个四位的素数a,b。(没有前导零) 输出格式   对于每个样例,输出 阅读详情

相关推荐

IAR9.X半主模式异常解析:为何硬件复位后仍陷调试状态?

本文深入解析了IAR 9.X半主模式异常问题,探讨了硬件复位后MCU仍陷调试状态的原因及解决方案。通过分析半主模式工作机制,提供了禁用半主功能、重写低级IO函数等具体方法,并给出了版本兼容性处理和调试与量产配置分离的优化建议,帮助开发者有效解决复位异常问题。

oo7890的博客 605

百练OJ-Prime Path

#include&lt;queue&gt; #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;math.h&gt; #include&lt;cstring&gt; using namespace std; vector&lt;int&gt;primes; void makeprimes() { primes.push_ba...

qq_34392405的博客 328

RobotStudio码垛机器人创建过程

首先导入一个IRB2600机器人,移动夹具的本地坐标原点,使原点位置为顶面中心(与法兰盘连接的部位)。对齐夹爪Smart组件的本地坐标和机器人末端法兰盘的坐标...

个人博客 1万+

[kuangbin带你飞]专题一 简单搜索 F - Prime Path poj 3126

F - Prime Path Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3126 Appoint description:  System Crawler  (2015-11-08) Descript

qq1319479809的博客 487

手把手教你用ADuC845 DAC调试恒流电路(附实测数据与避坑指南)

本文详细介绍了如何利用ADuC845 DAC调试恒流电路,包括硬件搭建、DAC输出校准、低端死区问题解决方案及系统级调试技巧。通过实测数据和避坑指南,帮助工程师快速解决非线性输出和稳定性问题,提升电路性能。

life6的博客 337

ROS四旋翼无人机快速上手指南(3):工作环境文件框架与各功能包整体讲解

这一章节主要讲解无人机平台的控制文件架构和各个功能包组成和用法。

The Road of Engineer 2406

poj 3126 Prime Path([kuangbin带你飞]专题一 简单搜索)

题目连接:题目 题目大意:两个素数,每次可以改变前一个数的某一位,并且改变完还是素数,求从第一个数变成第二个数的最小步数 解题思路:能读懂题目就差不多能知道用bfs,枚举每一位,改变数,看是不是素数, #include #include #include #include #include #include #include #include using namespace std; boo

Strive 565

Matlab生成dsp程序——环境搭建(c2000系列[F28335、F2837x]都适用)

一、环境配置 (28377D的操作是一模一样就是硬件设置的时候需要选择28377d进行相应的设置) 1)matlab与ccs联合编译需要安装对应的matlab版本和ccs版本,且只有2016a以上的matlab支持c2000系列。具体对应关系参照: http://software-dl.ti.com/ccs/esd/documents/ccs_matlab.html 2)matlab版本确定之后,还需要安装有关于ccs的相关软件: I、TI Code Composer Studio xxxx(对应

qq_42151264的博客 2万+

[kuangbin带你飞]专题一 简单搜索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 offices. — It is a matter of security to change such things every now and then, to

ilove_Moretz的博客 304

【光子 AI 】LangGraph:Graph = 有向有环图 + 状态机实现原理详解:数据结构模型与核心算法代码实现逻辑解析

LangGraph核心原理解析:基于Pregel模型的消息传递图,结合有向有环图和状态机特性。关键实现包括:1) 共享状态机制(TypedDict/Pydantic模型)配合Reducer逻辑实现状态更新;2) 图拓扑结构通过邻接表维护节点和条件边;3) 检查点机制支持状态持久化和恢复。运行时采用Pregel循环算法,通过超级步迭代执行节点函数、应用状态归约、条件路由下一批节点,同时支持人工中断和异步并行处理。该架构使LangGraph具备循环执行、条件跳转等DAG无法实现的动态流程控制能力。

AI天才研究院 1036

PostgreSQL性能优化:如何搭建监控体系?

本文系统讲解如何构建企业级PostgreSQL监控体系,覆盖四大核心维度:基础设施层、数据库实例层、会话与查询层、对象与存储层。通过Prometheus+Grafana+ELK等工具链实现指标采集、可视化与告警闭环管理,详细解析关键监控指标、部署方法及告警策略设计,帮助DBA快速定位慢查询、连接泄漏、存储膨胀等典型故障,构建"看得见、管得住、防得早"的数据库运维能力。

数据知道的博客 2234

POJ 3126 Prime Path(kuangbin带你飞 专题一:简单搜索)

刚开始以为要输出变换路径...惆怅好久,后来发现并不需要,然后果断AC。 标记数组先把非素数的都标记为用过...然后进行bfs,删除某一位的数字然后填0-9... #include #include #include #include #include #include #include #include using namespace std; int used[20000],n,k,min

530

C#串口通信从入门到精通(27)——高速通信下解决数据处理慢的问题(20ms以内)

本文介绍如何在高速通信情况下解决串口接收的数据来不及处理的问题,由高级工程师多年经验总结而成,绝对是串口领域的实战经验!

qq_34059233的博客 6813

[kuangbin带你飞]专题一 简单搜索-F - Prime Path POJ - 3126

Prime PathDescriptionThe 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.— I...

帅帅的博客 295

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[kuangbin带你飞]专题一 简单搜索F - Prime Path(POJ 3126)

F - Prime Path Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3126 Appoint description:  System Crawler  (2015-11-08) Descript

ZZ_AC的博客 1174

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

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

kuangbin专题一:F题,POJ3126:Prime Path

POJ3126:Prime Path kuangbin专题一:F题 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 ...

aboc43983的博客 205

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

[kuangbin]专题一 简单搜索 F - Prime Path

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

寻梦道理的专栏 457

Prime Path POJ - 3126 bfs

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

qq_35423505的博客 234

【kuangbin带你飞专题】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 matte...

weixin_40215561的博客 424

[kuangbin] POJ 3126 Prime Path

搜索就比较简单了,用bfs搜,每次只变化一位,每次枚举每一位的可能变化,若是质数并且还没被搜过则入队,每次搜索的时间复杂度最大等于1000~10000中质数的个数,所以时间复杂度ok,不会超时,具体看代码。这题算是筛质数与搜索结合。

qq_73329099的博客 237

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

[kuangbin带你飞]专题一 简单搜索 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 offices.  — It is a ...

weixin_40829921的博客 187

(kuangbin带你飞--简单搜索)Prime Path POJ3126

原题目: 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 ...

weixin_43791787的博客 220

专题一 简单搜索【Kuangbin】

kuangbin专题 POJ1321 棋盘问题【DFS】 POJ2251 Dungeon Master【3维空间BFS】 POJ3278 Catch That Cow【BFS】 POJ3279 Fliptile【状态压缩+DFS】 POJ1426 Find The Multiple【BFS】 POJ3126 Prime Path【BFS】 POJ3087 Shuffle'...

逐梦者 1224
上一篇: kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279
下一篇: kuangbin专题 专题一 简单搜索 Shuffle'm Up POJ - 3087
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值