Prime Path POJ-3126
— 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
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0 题目大意:n组输入,每一组输入两个四位质数,对第一个质数进行转换,使之变为第二个质数,每次只能更改一个数中某一位的数,且变动一位后的数也是质数才符合要求。输出最
少经过几次转换,能转换成第二个质数,保证输入合法。
思路:我这里采用了比较笨的方法,用结构体存储每一位上的数,以及总数,建立结构体队列,进行广度优先搜索,通过数组存储转换次数信息。(主要是自己比较菜,获取某个
数中每一位的数不够熟练,后续会补上用整型解决问题的思路。)还有一个问题是判断某数是否为质数的函数,这里我参考了大佬的博客(https://blog.csdn.net/huang_miao_xin/article/details/51331710),
很巧妙的判断质数的算法,值得学习。代码如下:
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
int const max_n = ;
using namespace std;
int jug[max_n];//记录信息数组
struct node {
int a, b, c, d;//千位,百位,十位,个位
int sum;//数的大小
};
queue<node> q;
int test(node x)//由各个数位的数求大小
{
return x.a * + x.b * + x.c * + x.d;
}
bool judge(int x)//是否为质数
{//关于这种方法判断质数的解释,6x,6x+2,6x+4一定被2整除,6x+3一定被3整除,这样就只用判断6x+1和6x+5的数了。而当循环以6位单位跨进时,就是6x-1和6x+1的情况了。更详细的讲解可以去大佬博客看
if (x == || x == )return true;//特判2,3两个质数
if (x % != && x % != )return false;
int tmp = sqrt(x);
for (int i = ; i <= tmp; i += )
{
if (x%i == || x % (i + ) == )return false;
}
return true;
}
int bfs(int x, int y)
{
node sx;
sx.a = x / ;
sx.b = x / % ;
sx.c = x / % % ;
sx.d = x % % % ;
sx.sum = x;
q.push(sx);//入队
memset(jug, , sizeof(jug));
jug[x] = ;
while (!q.empty())
{
node s = q.front(); q.pop();
if (s.sum == y)return jug[s.sum] - ;
for (int i = ; i < ; i++)//千位进行转换
{
node p;
p.a = s.a, p.b = s.b, p.c = s.c, p.d = s.d;
p.a = i;
p.sum = test(p);
if (judge(p.sum) && jug[p.sum] == )
{
jug[p.sum] = jug[s.sum] + ;
q.push(p);
}
}
for (int i = ; i < ; i++)//百位进行转换
{
node p;
p.a = s.a, p.b = s.b, p.c = s.c, p.d = s.d;
p.b = i;
p.sum = test(p);
if (judge(p.sum) && jug[p.sum] == )
{
jug[p.sum] = jug[s.sum] + ;
q.push(p);
}
}
for (int i = ; i < ; i++)//十位进行转换
{
node p;
p.a = s.a, p.b = s.b, p.c = s.c, p.d = s.d;
p.c = i;
p.sum = test(p);
if (judge(p.sum) && jug[p.sum] == )
{
jug[p.sum] = jug[s.sum] + ;
q.push(p);
}
}
for (int i = ; i < ; i++)//个位进行转换
{
node p;
p.a = s.a, p.b = s.b, p.c = s.c, p.d = s.d;
p.d = i;
p.sum = test(p);
if (judge(p.sum) && jug[p.sum] == )
{
jug[p.sum] = jug[s.sum] + ;
q.push(p);
} }
}
return ;
}
int main()
{
int t; scanf("%d", &t);
while (t--)
{
int m, n;
scanf("%d %d", &m, &n);
while (q.size())q.pop();
if (m == n) { printf("0\n"); continue; }
else
{
int x = bfs(m, n);
if (x == )printf("Impossible\n");
else printf("%d\n", x);
}
}
return ;
}
Prime Path POJ-3126的更多相关文章
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
- Mathematics:Prime Path(POJ 3126)
素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的 ...
- kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126
题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
随机推荐
- 腾讯面试Android高级岗,居然被一个多线程基础面倒了?
前言 一个在深圳从事开发五年的老友一个月前从原公司辞职后,昨天去腾讯总部面试Android高级岗,一面的时候,自我介绍后,陆陆续续问了很多问题,有着五年的从业经验很多项目开发的技术问题都回答的很通顺, ...
- 通过async与await实现高效并发
withTimeoutOrNull: 在上一次https://www.cnblogs.com/webor2006/p/12010388.html中对于协程的超时机制进行了一个学习,上次用的是withT ...
- wordpress中文目录出现“有点尴尬诶!该页无法显示"
原因不详,可能是.htaccess.网上说删除后再更新固定链接会再生成,但是我没有.我又把原来的.htaccess上传后更改固定链接为“数字型”,测试后可以正常浏览. 然后又再更改为原来的“日期和名称 ...
- 记录一次群答问:requests获取cookie
问题: 为了测试,写的sever,下面仅为set cookie的部分代码 response = make_response('{"code":9420, "msg&quo ...
- 《浅谈我眼中的express、koa和koa2》好文留存+笔记
原文 :三英战豪强,思绪走四方.浅谈我眼中的express.koa和koa2 一.回调大坑怎么解决呢? 1.es5可以利用一下第三方库,例如 async 库, 2.或者单纯使用 connect中间件 ...
- contest5 CF991 div2 ooooxx ooooox ooooox
题意 div2D 给出一个棋盘, 有一些点不能放, 总共\(2\)排, 长度\(n(\le 100)\) 在上面空位摆放\('L'\)字形的牌, 问最多能放几个 例如: 00X00X0XXX0 0XX ...
- 搜索法 | 1103 dfs搜索:符合条件的多项式
其实这题我已经写过两遍了,但都是在看过算法笔记的情况下写的.方法不难,只要能想出来. 找到一个项数为k,每项为p次幂,和为n,并且在有多个结果的情况下要求数字之和最大的一个多项式.如果数字之和相等.还 ...
- 第03组 Beta冲刺(4/4)
队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...
- 使用webpack.optimize.CommonsChunkPlugin提供公共代码
在webpack4里使用webpack.optimize.CommonsChunkPlugin时,报错,webpack4删除了常用的 CommonsChunkPlugin ,提示我们用config.o ...
- centos虚拟机扩展磁盘空间(经历无数坑,血一样总结,史上最全)
第一步 在vmware中将虚拟机关机后,鼠标右键设置,直接点击扩展加自己想要扩展的数量就可以了,这个比较简单不多说. 2 第二步 设置后进系统查看空间大小变化,实际并没有什么变化,我用的命令是df - ...