Prime Path

原文是English 这里直接上中文了

Descriptions:

给你两个四位的素数a,b。
a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变。
请你计算a最少经过多少次上述变换才能变成b。
例如:1033 -> 8179 
1033 
1733 
3733 
3739 
3779 
8779 
8179
最少变换了6次。
Input

第一行输入整数T,表示样例数。 (T <= 100) 
每个样例输入两个四位的素数a,b。(没有前导零) 
Output

对于每个样例,输出最少变换次数,如果无法变换成b则输出"Impossible"。

Sample Input

  1. 3
  2. 1033 8179
  3. 1373 8017
  4. 1033 1033

Sample Output

  1. 6
  2. 7
  3. 0

题目链接

https://vjudge.net/problem/POJ-3126

两个点,每一步都是素数,步数最小,针对这个,先列出一个素数表,对于每一位的变化都搜一下即可,个人写搜索,习惯用优先队列

AC代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #include <sstream>
  15. #define mod 1000000007
  16. #define eps 1e-6
  17. #define ll long long
  18. #define INF 0x3f3f3f3f
  19. #define ME0(x) memset(x,0,sizeof(x))
  20. using namespace std;
  21. int T,n,m;
  22. int isprime[];//素数表
  23. int vis[];//标记
  24. struct node
  25. {
  26. int num,money;
  27. bool operator <(const node &c) const//步数小的先出队
  28. {
  29. return money>c.money;
  30. }
  31. } now,next;
  32. void eratos(int x)//求素数表
  33. {
  34. for(int i=; i<=x; ++i)
  35. isprime[i]=true;
  36. isprime[]=isprime[]=false;
  37. for(int i=; i<=x; ++i)
  38. {
  39. if(isprime[i])
  40. {
  41. int j=i+i;
  42. while(j<=x)
  43. {
  44. isprime[j]=false;
  45. j+=i;
  46. }
  47. }
  48. }
  49. }
  50. void bfs()
  51. {
  52. priority_queue<node>q;
  53. now.num=n,now.money=;
  54. q.push(now);
  55. vis[n]=;
  56. int f=;
  57. // cout<<now.num<<endl;
  58. // cout<<2<<endl;
  59. while(!q.empty())
  60. {
  61. char x[];
  62. // cout<<1<<endl;
  63. now=q.top();
  64. q.pop();
  65. if(now.num==m)
  66. {
  67. f=;
  68. cout<<now.money<<endl;
  69. return;
  70. }
  71. for(int i=; i<; ++i)
  72. {
  73. sprintf(x,"%d",now.num);
  74. for(int j=; j<; ++j)
  75. {
  76. if(i==&&j==)//千位不允许为0
  77. continue;
  78. if(i==)//四种情况,分别针对"个十百千"位的变换
  79. next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
  80. else if(i==)
  81. next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
  82. else if(i==)
  83. next.num=j*+(x[]-'')*+(x[]-'')*+(x[]-'');
  84. else if(i==)
  85. next.num=j+(x[]-'')*+(x[]-'')*+(x[]-'')*;
  86. if(isprime[next.num]&&!vis[next.num])//这个数是素数且没被标记过
  87. {
  88. vis[next.num]=;
  89. next.money=now.money+;
  90. q.push(next);
  91. }
  92. }
  93. }
  94. }
  95. if(f==)
  96. {
  97. cout<<"Impossible"<<endl;
  98. return;
  99. }
  100. }
  101. int main()
  102. {
  103. eratos();//10005以内的素数表
  104. cin>>T;
  105. while(T--)
  106. {
  107. ME0(vis);
  108. cin>>n>>m;
  109. bfs();
  110. }
  111. }

【POJ - 3126】Prime Path(bfs)的更多相关文章

  1. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  2. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  3. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  4. POJ 3126:Prime Path(素数+BFS)

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  5. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  7. poj3216 Prime Path(BFS)

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

  8. 【POJ 1273】Drainage Ditches(网络流)

    一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...

  9. BZOJ 2296【POJ Challenge】随机种子(构造)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2296 [题目大意] 给出一个数x,求一个10的16次以内的数使得其被x整除并且数字包含 ...

随机推荐

  1. Missing artifact com.sun.jmx:jmxri:jar:1.2.1的解决方法

    maven项目添加log4j-1.2.15依赖出现Missing artifact com.sun.jmx:jmxri:jar:1.2.1错误 解决方法一:修改log4j.jar的版本为1.2.16或 ...

  2. pandas聚合和分组运算之groupby

    pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个键(可以是函数.数组或DataFrame列名)拆分pandas对象.计算分 ...

  3. Hadoop-No.2之标准文件格式

    标准文件格式可以指文本格式,也可以指二进制文件类型.前者包括逗号分隔值(Comma-Separated Value,CSV和可扩展的标记语言文本(Extensible Markup Language. ...

  4. mysql explain 执行计划详解

    1).id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询.   2).select_type列常见的有: A:simple ...

  5. java+文件批量下载

    这篇文章主要介绍了Java实现批量下载选中文件功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下 1.在action中定义变量 private List<String> downLoa ...

  6. javaScript第一篇

    什么中DOM: DOM是一套对文档内容进行抽象各概念化的方法; 例如:我们对别人说:“猫在沙发上!”:别人听到的不会是“狗已经跑了”:这是因为人类对已有的事物有了一套公有的认识;再比如,有人问你,“左 ...

  7. 如何在VMware软件上安装Red hat(红帽)Linux6.9操作系统

    本文介绍如何在VMware软件上安装Redhat(红帽)Linux6.9操作系统 首先需要准备 VMware软件和Redhat-Linux6.9操作系统的ISO系统镜像文件包(这里以linux6.9为 ...

  8. 2019icpc南京网络赛 F 主席树

    题意 给一个\(n\)的全排列数组\(a\),求一个递推数组每一项的值:\(ans[i]=ans[j]+1\),\(j\)为\(a[pos[i]-k]到a[pos[i]+k],(pos[i]为i在数组 ...

  9. Zookeeper执行原理的详细概述

    文章作者:Holy Null,来源:http://holynull.leanote.com/post/Zookeeper,非常感谢作者提供如此优秀的原创文章,作者通过俩个月的努力将<Hadoop ...

  10. python数据可视化示例柱状图

    from matplotlib import pyplot as plt import platform import pandas from pathlib import Path # 根据不同的平 ...