POJ3126 Prime Path

  一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径,

而且代码实现起来特别麻烦,后来搜了一下解题报告,才发现是bfs().

  想想也是,题目其实已经提示的很清楚了,求最短的路径,对于每一个数,每次可以把4位中的任意一位,  换成与该位不相同的0-9中的任意一位,对于迷宫类

bfs每次转移数为上下左右四个状态,而此题就相当于每次可以转移40个状态(其实最低位为偶数可以排除,不过题目数据量较小,就不剪枝了),每次转移的这40个状态,都可能得到最小路径,对于每次转移得到的素数,标记一下used,同时标记一下深度(即所走过的步数)root,从起点m出发,每次进行40个方向的状态转移,直至找到终点n或者队列取空返回无解(-1);

    一开始把rep(j,0,10)写成了rep(j,0,9),wa了半天,这种小细节一定要注意.

  1. /*
  2. * Created: 2016年03月30日 08时06分49秒 星期三
  3. * Author: Akrusher
  4. *
  5. */
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <string>
  14. #include <vector>
  15. #include <deque>
  16. #include <list>
  17. #include <set>
  18. #include <map>
  19. #include <stack>
  20. #include <queue>
  21. #include <numeric>
  22. #include <iomanip>
  23. #include <bitset>
  24. #include <sstream>
  25. #include <fstream>
  26. using namespace std;
  27. #define rep(i,a,n) for (int i=a;i<n;i++)
  28. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  29. #define in(n) scanf("%d",&(n))
  30. #define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
  31. #define inll(n) scanf("%I64d",&(n))
  32. #define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
  33. #define inlld(n) scanf("%lld",&(n))
  34. #define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
  35. #define inf(n) scanf("%f",&(n))
  36. #define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
  37. #define inlf(n) scanf("%lf",&(n))
  38. #define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
  39. #define inc(str) scanf("%c",&(str))
  40. #define ins(str) scanf("%s",(str))
  41. #define out(x) printf("%d\n",(x))
  42. #define out2(x1,x2) printf("%d %d\n",(x1),(x2))
  43. #define outf(x) printf("%f\n",(x))
  44. #define outlf(x) printf("%lf\n",(x))
  45. #define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
  46. #define outll(x) printf("%I64d\n",(x))
  47. #define outlld(x) printf("%lld\n",(x))
  48. #define outc(str) printf("%c\n",(str))
  49. #define pb push_back
  50. #define mp make_pair
  51. #define fi first
  52. #define se second
  53. #define SZ(x) ((int)(x).size())
  54. #define mem(X,Y) memset(X,Y,sizeof(X));
  55. typedef vector<int> vec;
  56. typedef long long ll;
  57. typedef pair<int,int> P;
  58. const int dx[]={,,-,},dy[]={,,,-};
  59. const int INF=0x3f3f3f3f;
  60. const ll mod=1e9+;
  61. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  62. const bool AC=true;
  63.  
  64. bool used[];
  65. int root[];
  66. int s[];
  67. bool prime(int n){ //素数测试
  68. for(int i=;i*i<=n;i++){
  69. if(n%i==) return false;
  70. }
  71. return n!=; //1是例外
  72. }
  73. int bfs(int m,int n){ //m相当于起点,n相当于终点,此题就转化为求每次可以走40个方向的迷宫问题的最短距离;
  74. queue <int> que;
  75. que.push(m);
  76. int q,now;
  77. while(!que.empty()){
  78. q=que.front();
  79. que.pop();
  80. if(q==n) return root[q];
  81. s[]=q/;s[]=(q/)%;
  82. s[]=(q%)/;s[]=q%;
  83. rep(i,,){ //每次改变4位数中的一位
  84. rep(j,,){ //j>=0&&j<=9,此处不能写成rep(j,0,9);
  85. if(j!=s[i]){
  86. now=q-(s[i]-j)*pow(,i);
  87. if((!used[now])&&(prime(now))&&(now>)){ //注意最高为不能是0
  88. root[now]=root[q]+;//步数加1
  89. used[now]=true; //标记该素数已被使用
  90. que.push(now);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return -; //从起点m出发所有能够转移到的素数都已用完,没找到解,就返回-1
  97. }
  98. int main()
  99. {
  100. int t,a,b;
  101. in(t);
  102. while(t--){
  103. in2(a,b);
  104. fill(used,used+,false);
  105. mem(root,);
  106. int ans=bfs(a,b);
  107. if(ans!=-) out(ans);
  108. else printf("Impossible");
  109. }
  110. return ;
  111. }

POJ3126 Prime Path (bfs+素数判断)的更多相关文章

  1. POJ3126 Prime Path —— BFS + 素数表

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  3. POJ3126 Prime Path(BFS)

    题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

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

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  5. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  6. poj3126 Prime Path 广搜bfs

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

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

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

  8. CD0J/POJ 851/3126 方老师与素数/Prime Path BFS

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 Descri ...

  9. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

随机推荐

  1. Delphi调用WINAPI时到底应该是指针还是结构体(注意是Delphi变量本身就是指针)

    看MSDN,GetWindowRect的说明如下: BOOL WINAPI GetWindowRect( _In_  HWND   hWnd, _Out_ LPRECT lpRect // 注意,没* ...

  2. java学习面向对象之设计模式之单例模式

    就像上一节当中我们讲到的数组工具集一样,如果我们把他看作一个类,来应用,不阻止他new函数的话,这个类我们在整个过程当中我们只是用他来当一个工具.假如每次用都要new一下产生一个新对象的话,就会显得整 ...

  3. Javascript之return

    做表单验证的时候,除了错误提示之外,还要做的一点就是避免表单提交. 如果避免表单提交呢? 有一个方法很简单,就是return 我们来看一下代码: $(".make_sure").c ...

  4. 【模拟】Codeforces 705A Hulk

    题目链接: http://codeforces.com/problemset/problem/705/A 题目大意: 给一个数N(N<=100),N=1时输出"I hate it&qu ...

  5. 使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

    原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: ...

  6. hdu 4322 最大费用流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 #include <cstdio> #include <cstring> ...

  7. 【bzoj2434】[Noi2011]阿狸的打字机

    题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的:l 输入小写字 ...

  8. Ubuntu 添加桌面快捷方式

    使用Linux Desktop Entry创建 ~$ cd ~/Desktop/ ~$ vim AndroidStudio.desktop 插入以下代码 [Desktop Entry] Encodin ...

  9. 设计模式21---设计模式之享元模式(Flyweight)(结构型)

    1.讲解享元模式(结构型) 1.1享元模式定义 运用共享技术有效地支持大量细粒度对象. 享元:把内部状态共享出来 1.2享元模式要点 重点在于分离变与不变. 把一个对象的状态分为内部状态和外部状态,内 ...

  10. ZOJ 1301 The New Villa (BFS + 状态压缩)

    题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...