F - 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 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
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<queue>
#define maxn 10000
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m;
bool prime[maxn +5];
int dis[maxn + 5];
struct p{int x,step;
}cur, nxt;
void dfs()
{queue<p> q;cur.x = n;cur.step = 0;q.push(cur);while(q.size()){cur = q.front();q.pop();if(cur.x == m){cout << cur.step <<endl;return ;}nxt.step = cur.step + 1;for(int i = 0; i < 4; i++){for(int j = 0; j <= 9; j++){if(i == 0)nxt.x = cur.x - cur.x % 10 + j;if(i == 1)nxt.x = cur.x + (j - cur.x%100/10)*10;if(i == 2)nxt.x = cur.x + (j - cur.x/100 % 10)*100;if(i == 3 && j != 0)nxt.x = cur.x + (j - cur.x/1000) *1000;if(prime[nxt.x] && !dis[nxt.x]){dis[nxt.x] = 1;q.push(nxt);}}}}cout << "Impossible" <<endl;return ;
}
int main()
{int t;cin >> t;memset(prime, true, sizeof prime);for(int i = 2; i <=100; i++){for(int j = 1; j *i <= 10000; j++){prime[i * j] = false;}}prime[1] = false;while(t--){memset(dis, 0, sizeof dis);cin >> n >> m;if(n == m)cout << '0' << endl;else {dis[n] = 1;dfs();}}return 0;
}
F - Prime Path相关推荐
- F - Prime Path POJ - 3126
F - Prime Path POJ - 3126 题意修改一个四位数质数的某一位,使得该质数又变为一个质数,求从当前数变为目标质数的最少变化次数. bfs枚举每一位的每种变化 #include< ...
- Prime Path(bfs)广度优先搜索
题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- poj3126 Prime Path BFS
点击打开链接 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26158 Accepted: 143 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- POJ 3126 Prime Path(BFS + 素数打表)
题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...
- 1646. Prime Path
单点时限: 2.0 sec 内存限制: 256 MB The ministers of the cabinet were quite upset by the message from the Chi ...
- POJ 3126 - Prime Path + Python(BFS)
()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...
最新文章
- java script 月日年转年月日_js的如何进行日期格式转换成年月日
- 日志分析系统分类有哪些_SEO优化中网站日志起到的重要性作用
- vc中怎么使用SendMessage自定义消息函数
- LeetCode 1617. 统计子树中城市之间最大距离(枚举所有可能+图的最大直径)
- 防水耐脏,超大容量双肩包,限时拼团仅需49元
- 归并排序,我举个例子你就看懂了
- SQL Server海量数据查询代码优化建议
- JAVA计算机毕业设计大学生旅游拼团网站计算机(附源码、数据库)
- idea 破解版安装
- 编译原理04-自顶向下语法分析方法
- Qt调用工业相机之相机的触发模式及代码实现
- ASF文件格式详解(1)
- Android项目:基于Android手机校园外卖订餐系统(计算机毕业设计)
- 股票基本知识入门提纲
- 机器学习入门系列05,Classification: Probabilistic Generative Model(分类:概率生成模型)
- 学习vue-vben-admin遇到的问题(一)
- IDEA: 全局搜索 、全局查找
- Axure交互之链接行为
- 从零开始学数据结构和算法:java从大到小排列数组
- 我推荐亲戚家小孩学编程,差点被打一顿!
热门文章
- 20150217 IMX257实现GPIO-IRQ中断按键驱动程序
- 做基础产品的体会【转载】
- python知识点2--正则表达式【转载】
- [转载]解决mysql“Access denied for user 'root'@'localhost'”
- Django+Scrapy结合使用并爬取数据入库
- 代码审计 phpcmsv9 MVC介绍
- 位运算:二进制中1的个数
- 小菜学Flex2(二 currentState初步使用)
- 【Tensorflow深度学习】Tensorflow2.0GPU版极简安装
- 读取XML文件时,总报“前言中不允许有内容”错误-分析