POJ 3126 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物理不兼容,也揭示了内存选购中频率、时序与散热之间的真实关系。本文以该标准为线索,梳理引脚定义、关 阅读详情

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14799 Accepted: 8345

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

题目链接:http://poj.org/problem?id=3126

题目大意:给定两个四位数的质数a,b,由a开始,每次变换一位上的数字得到一个新的四位质数,直到变换得到b,求最少变换的次数。如果没有解,输出“Impossible”。

解题思路:典型BFS暴力,预处理一下素数筛就行,改变位上的数字时用数组存储该四位数。

代码如下:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n,m,prm[100000],vis[100000];
struct node
{
    int x,cnt;
};
void get_prime()    //得到四位数的素数晒
{
    memset(prm,0,sizeof(prm));
    for(int i=1000;i<=9999;i++)
    {
        bool p=true;
        for(int j=2;j*j<=i;j++)
        {
            if(i%j==0)
            {
                p=false;
                break;
            }
        }
        if(p)
            prm[i]=1;
    }
}
int bfs()
{
    queue<node>q;
    node sd;
    sd.x=n,sd.cnt=0;
    q.push(sd);
    while(!q.empty())
    {
        node nn,nd=q.front();
        q.pop();
        if(nd.x==m)
            return nd.cnt;
        int a[4];
        int tm=nd.x;
        for(int i=0;i<4;i++)
        {
            a[i]=tm%10;
            tm/=10;
        }      
        for(int i=0;i<4;i++)         //枚举每一位上的数字
        {           
            for(int j=0;j<=9;j++)        //枚举当前位上要改变的数字
            {
                if(j==a[i] || i==3&&j==0)continue;
                int cur;
                if(i==0)
                    cur=a[3]*1000+a[2]*100+a[1]*10+j;
                else if(i==1)
                    cur=a[3]*1000+a[2]*100+j*10+a[0];
                else if(i==2)
                    cur=a[3]*1000+j*100+a[1]*10+a[0];
                else
                    cur=j*1000+a[2]*100+a[1]*10+a[0];
                if(vis[cur] || !prm[cur])continue;   //如果下一个数字被访问过或者不是质数,跳过
                vis[cur]=1;
                nn.x=cur;
                nn.cnt=nd.cnt+1;
                q.push(nn);
            }
        }     
    }
    return -1;
}
int main(void)
{
    int t;
    get_prime();
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        vis[n]=1;
        int ans=bfs();
        if(ans==-1)
            printf("Impossible\n");
        else
            printf("%d\n",ans);
    }
}


基于YOLOv8的多类别检测模型融合实战:从数据集重构到模型训练 本文详细介绍了如何将多个单一类别的YOLOv8检测模型融合为一个高效的多类别检测模型。通过重构数据集、利用预训练模型生成新标签,并重新训练,解决了多模型并行带来的速度慢、资源占用高和部署复杂等问题。实战流程涵盖数据准备、标签生成、模型训练与评估,旨在帮助开发者构建性能优异的YOLOv8多类别检测模型。 阅读详情

相关推荐

STM32L051低功耗模式下LPUART的使用

STM32L0系列低功耗UART配置代码清单及注释,大家可参考学习使用。

嵌入式工程师 2100

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

Qwen 三代进化全景对比:从 Qwen2.5 到 Qwen3 再到 Qwen3.5

本文对比了阿里巴巴通义千问三代大模型(Qwen2.5、Qwen3、Qwen3.5)的技术演进。18个月内,模型在架构、训练数据和能力上实现显著突破:从纯稠密结构(Qwen2.5)发展为混合注意力架构(Qwen3.5),支持语言从29种扩展至201种,并实现原生多模态融合。关键进展包括引入MoE架构、Gated DeltaNet混合注意力机制(计算效率提升10倍)、以及从独立模态到早期视觉语言联合训练的转变。模型阵容从7个通用尺寸发展为8+个混合架构,最大参数量达397B,上下文窗口扩展至256K(托管版1M

m0_47999117的博客 1万+

(BFS)POJ3126Prime Path

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

Z7784562的博客 1519

防止通达信公式被盗?手把手教你用DLL加密+互联网验证(含易语言后台管理)

本文详细介绍了如何通过DLL加密与互联网验证技术保护通达信公式安全。从DLL加密实现、设备绑定到易语言后台管理,提供了一套完整的防护方案,有效防止公式被盗用。重点讲解了互联网验证登录和绑定设备等核心技术,帮助量化交易者守护核心资产。

weixin_29323551的博客 500

别再瞎改网络结构了!解决YOLO小目标检测痛点,我用这招把mAP提了15%

本文基于我7年工业PCB缺陷检测、无人机航拍、安防监控项目的一线落地经验,针对YOLO小目标检测最痛的「漏检率高、特征丢失、精度难提升」三大核心问题,拆解出。本文全程配套小目标特征丢失原理图、分块训练流程图、切图前后效果对比图、mAP提升柱状图、多方案性能对比表等可视化内容,从YOLO天生不擅长小目标的底层根源、方案核心原理、可直接复制的代码实现,到工业场景实测验证、新手必踩的10个坑位避坑指南,全链路讲透。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 386

Prime Path POJ - 3126 bfs

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

qq_35423505的博客 234

【2022网易雷火】游戏研发笔试-AC代码及题目分享

3个小时,4道题目,雷火的题目不在于难,更在于对细节的把控和写大模拟的耐心,花了一个半小时,AC了3.85,直接交卷退了。第一题AC,第二题AC,第三题85%,第四题AC。下面分享一下大致的题意和对应的代码,就不多说了,最近很累(代码可能会有贴错的情况,如有望指正,感谢~)。 T1-小Y抽奖 题意 n行m列个奖励格子,每次抽奖消耗一个道具。每个格子奖励的数量有限且各不相司。每个格子被抽到一次,剩余的奖励数量-1。剩余的奖励数量为0时,则不会再抽到该格子。小Y抽了好多次,但一直没有抽到最想要的..

米莱虾 9265

BFS POJ 3126 Prime Path

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

weixin_33717117的博客 137

YOLOv11改进策略【卷积层】| CVPR2025 GBConv轻量门控瓶颈卷积 低秩瓶颈降参 + 门控聚焦形态,强效捕捉纤细目标

裂缝形态不规则、纹理纤细,传统卷积参数大、计算冗余,且无法动态聚焦裂缝形态;背景噪声强易干扰特征提取,需要轻量且能自适应聚焦裂缝形态的卷积单元,因此提出轻量门控瓶颈卷积 GBC。

Limiiiing的博客 851

ZYNQ microblaze boot.bin固化步骤

一,ZYNQ移植软核的原理FPGA规模巨大而且功能复杂,因此设计的每一个部分都从头开始是不切实际的。一种解决的办法是:对于较为通用的部分可以重用现有的功能模块,而把主要的时间和资源用在设计中的那些全新的、独特的部分。这就像是你在开发应用程序的时候就不用直接去写驱动物理硬件的代码,而是直接调用Windows提供的API就行了。

wangjie36的博客 2780

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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Prime Path POJ - 3126 bfs

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

Tizzi的博客 911

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

POJ 3126 Prime Path (BFS)

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

ding_ning123的博客 177

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

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

POJ 3126 Prime Path(BFS)

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

Must so 392

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

【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 3126Prime Path--BFS

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

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

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

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

poj3126 prime path bfs

题目链接:http://poj.org/problem?id=3126 题意:给定两个范围在【1000,9999】的两个素数,问从一个素数到另外一个素数需要多少歩,每次只能改变一个数,且过渡的数必须是素数。例如:1033 ——1733——3733——3739——3779——8779——8179。 分析:很经典的广搜题,谈到广搜不得不说的是已经搜索过的状态的标记,可以用visit数组,然后就是

Na_OH的博客 525

POJ3126——BFS——Prime Path

http://poj.org/problem?id=3126 大意:从一个素数到另一个素数,每次只能改变一个数,要求每次改变后的数都是素数,要求算出转换的最小次数,简单BFS,不过注意d要还原 /************************************************ * Author :Powatr * Created Time ...

dgjm4087的博客 157

[POJ]P3126 Prime Path[BFS]

[POJ]P3126 Prime Path Time Limit:1000MS Memory Limit:65536K Total Submissions:35230 Accepted:18966 Description The ministers of the cabinet were quite upset b...

dejuan4793的博客 181

POJ3126 Prime Path(BFS)

题目链接:POJ 3126 题意:每一组测试数据给定2个4位数的素数(第一位不为0),如1033,8179,要求每次只能变化一位,求从1033变化到8179需要多少步?这种情况的方案如下,需要6步 1033 1733 3733 3739 3779 8779 8179 Input One line with a positive number: the number of test cases (...

ha_ha_ha233的博客 383

POJ3126 Prime Path 打表+BFS

DescriptionThe 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 m

Bill_Utada的成长之路 1551

POJ 3126 Prime Path (BFS)

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 th

少年白马 193
上一篇: POJ 1426 Find The Multiple (dfs)
下一篇: poj 3414 Pots (DFS)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值