单点时限: 2.0 sec

内存限制: 256 MB

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.

输入格式
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).

输出格式
One line for each case, either with a number stating the minimal cost or containing the word Impossible.

样例
input
3
1033 8179
1373 8017
1033 1033
output
6
7
0
题意:就是每次仅改动上一轮数中的一位数之后,依旧为素数,最多改成到目标素数的次数。

/*
bfs将一轮可改为素数加进去,到了目标素数输出。
*/
#include<bits/stdc++.h>
#define MAX 10000
using namespace std;
typedef pair<string,int> P;
string n,m;
int t,sign[MAX],pri[MAX];//sign标记数是否被用过
void bfs() {queue<P>pq;string x;int step;pq.push({n,0});while(!pq.empty()) {P p=pq.front();pq.pop();x=p.first;step=p.second;if(x==m) {printf("%d\n",step);return ;}for(int i=0; i<=3; i++)for(int j=0; j<=9; j++)if(!(i==0&&j==0)) { //千位数字不能为0string str=x;str[i]='0'+j;if(str!=x&&!sign[atoi(str.c_str())]&&!pri[atoi(str.c_str())]) { //若变换后数字不想等且未使用过且为素数sign[atoi(str.c_str())]++;pq.push({str,step+1});}}}puts("Impossible");
}
int main() {for(int i=2; i<MAX; i++)for(int j=i; i*j<MAX; j++)pri[i*j]=1;cin>>t;while(t--) {fill(sign,sign+MAX,0);cin>>n>>m;sign[atoi(n.c_str())]++;bfs();}
}

1646. Prime Path相关推荐

  1. Prime Path(bfs)广度优先搜索

    题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  2. F - Prime Path POJ - 3126

    F - Prime Path POJ - 3126 题意修改一个四位数质数的某一位,使得该质数又变为一个质数,求从当前数变为目标质数的最少变化次数. bfs枚举每一位的每种变化 #include< ...

  3. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  4. poj3126 Prime Path BFS

    点击打开链接 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26158   Accepted: 143 ...

  5. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  6. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  7. F - Prime Path

    题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  8. POJ 3126 Prime Path(BFS + 素数打表)

    题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...

  9. POJ 3126 - Prime Path + Python(BFS)

    ()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...

最新文章

  1. java方法重载和重写在jvm_重载和重写在jvm运行中的区别(一)
  2. NFS的access denied
  3. 人的一生,到底在追求甚么?...
  4. jQuery 中jQuery/$的数据类型是什么?
  5. Mysql 5.7 报错 3534 错误
  6. 两个栈实现一个队列与两个队列实现一个栈
  7. activiti jbpm相关资源
  8. python怎么变成exe_Python怎样打包成exe?
  9. css清除浮动的集中方法
  10. 移除动态view android,请教Android,动态添加到控件能动态删除吗?
  11. web渗透测试思路浅谈-----漏洞发现及利用
  12. 计算机学院嘉年华标题,我院计算机系举办计算机嘉年华系列活动
  13. 性能测试篇 :Jmeter HTTP代理服务器录制压力脚本
  14. layui开关按钮,checkbox复选框开关按钮效果美化
  15. 问题1:VS2017:找不到 Windows SDK 版本10.0.17134.0
  16. ds18b20温度转换指令_【原创】基于单片机的DS18B20温度显示
  17. iPad商标之争对开发者的影响
  18. 浅谈四层交换机技术原理
  19. 软件工程网络15个人案例作业3(201521123045 郑子熙)
  20. 超详细的Spring Boot教程,搞定面试官!

热门文章

  1. 大内存PDF文件转Word免费之道
  2. 【Linux】工具使用
  3. mysql 规则引擎_为什么使用规则引擎?
  4. 自定义SmartRefreshLayout 下拉刷新Header(水浪+齿轮动画)
  5. android8、android13自适应图标适配
  6. 华为手机android是什么意思,华为手机的英文文件夹是什么意思?哪些可以删除?今天总算知道了...
  7. [CF1603D] Artistic Partition——欧拉函数,线段树优化DP
  8. 中国新冠疫苗首针将于1月15日接种,正确的运输和存储可保证其安全有效
  9. 秒杀限制人群,如何设计秒杀服务的限流策略?
  10. idea编译报错:Refer to the generated Javadoc files in ‘..\target\apidocs‘ dir. 解决