Language:
Default
Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11703   Accepted: 6640

Description

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

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

Sample Output

  1. 6
  2. 7
  3. 0

Source

题意:  给定两个素数(四位数),求第一个数经过几次转换可以得到第二个素数。

转换方式:是变换数中某一位的数字(第一位不能为零,其它的变换数字是0~~9),变换之后的数也为素数。

思路:bfs。搜索求最短路径,非常easy就想到广度优先搜索。由于广度优先搜索。第一次搜到得到的步数就是最少的步数。另外打素数表提高推断的时候的效率。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #pragma comment (linker,"/STACK:102400000,102400000")
  13. #define maxn 10005
  14. #define MAXN 2005
  15. #define mod 1000000009
  16. #define INF 0x3f3f3f3f
  17. #define pi acos(-1.0)
  18. #define eps 1e-6
  19. typedef long long ll;
  20. using namespace std;
  21.  
  22. bool ISprime[maxn];
  23. bool visit[maxn];
  24. int m,n,a,b,c,d;
  25.  
  26. struct Node
  27. {
  28. int p[4];//用数组存各位数
  29. int step;
  30. };
  31.  
  32. void prime() //素数筛法
  33. {
  34. for (int i=2;i<maxn;i++)
  35. {
  36. if (i%2)
  37. ISprime[i]=true;
  38. else
  39. ISprime[i]=false;
  40. }
  41. int m=sqrt(10010.0);
  42. for (int i=3;i<m;i++)
  43. {
  44. if (ISprime[i])
  45. {
  46. for (int j=i+i;j<maxn;j+=i)
  47. ISprime[j]=false;
  48. }
  49. }
  50. }
  51.  
  52. int bfs()
  53. {
  54. Node st,now;
  55. memset(visit,false,sizeof(visit));
  56. queue<Node>Q;
  57. while (!Q.empty())
  58. Q.pop();
  59. visit[m]=true;
  60. st.p[0]=m/1000;st.p[1]=(m/100)%10;st.p[2]=(m/10)%10;st.p[3]=m%10;
  61. // printf("%d %d %d %d\n",st.a[0],st.a[1],st.a[2],st.a[3]);
  62. st.step=0;
  63. Q.push(st);
  64. while (!Q.empty())
  65. {
  66. st=Q.front();
  67. Q.pop();
  68. if (st.p[0]==a&& st.p[1]==b&&st.p[2]==c&&st.p[3]==d)
  69. {
  70. return st.step;
  71. }
  72. for (int i=0;i<=3;i++)
  73. {
  74. for (int j=0;j<10;j++)
  75. {
  76. if (st.p[i]==j)
  77. continue;
  78. if (i==0&&j==0)
  79. continue;
  80. now.p[0]=st.p[0];
  81. now.p[1]=st.p[1];
  82. now.p[2]=st.p[2];
  83. now.p[3]=st.p[3];
  84. now.p[i]=j;
  85. int x=now.p[0]*1000+now.p[1]*100+now.p[2]*10+now.p[3];
  86. if (ISprime[x]&&!visit[x])
  87. {
  88. visit[x]=true;
  89. now.step=st.step+1;
  90. Q.push(now);
  91. }
  92. }
  93. }
  94. }
  95. return -1;
  96. }
  97.  
  98. int main()
  99. {
  100. prime();
  101. int cas;
  102. scanf("%d",&cas);
  103. while (cas--)
  104. {
  105. scanf("%d%d",&m,&n);
  106. a=n/1000;b=(n/100)%10;c=(n/10)%10;d=n%10;
  107. // printf("%d %d %d %d\n",a,b,c,d);
  108. int ans=bfs();
  109. if (ans==-1)
  110. printf("Impossible\n");
  111. else
  112. printf("%d\n",ans);
  113. }
  114. return 0;
  115. }
  116. /*
  117. 3
  118. 1033 8179
  119. 1373 8017
  120. 1033 1033
  121. */

Prime Path (poj 3126 bfs)的更多相关文章

  1. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

  2. Prime Path (POJ - 3126 )(BFS)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82697622     作者:Mercury_Lc 题目链接 题意:就是给你一个n, ...

  3. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  4. Prime Path(poj 3126)

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

  5. (广度搜索)A - Prime Path(11.1.1)

    A - Prime Path(11.1.1) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64 ...

  6. POJ 3126 Prime Path(筛法,双向搜索)

    题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...

  7. UVa 1599 Ideal Path (两次BFS)

    题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...

  8. 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)

    传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...

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

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

随机推荐

  1. 简单实用的下拉菜单(CSS+jquery)

    原文 简单实用的下拉菜单(CSS+jquery) 没什么可以说的,直接上例子 html+jquery代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  2. WTL---WxWidget---MFC 何去何从

    C++程序员打交道最多的就是MFC了,这个我不想多说,说来都是泪(C#年年更新,C++十年才出了一个featurePack还不是很好用) 现在另外两支队伍越来越庞大(所谓穷则思变,呵呵),一是WTL, ...

  3. 正则表达式验证数字、汉字、电话号码,email,整数,浮点数

    验证数字的正则表达式集 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...

  4. PopupWindow 的showatlocation参数解释

    showAtLocation(parent, gravity, x, y) 第一个参数指定PopupWindow的锚点view,即依附在哪个view上.第二个参数指定起始点第三个参数设置以起始点的右下 ...

  5. ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);

    1 <h2>Welcome</h2> <ul> <% for (int i = 0; i <= Convert.ToInt32(ViewData[&qu ...

  6. java--类继承和实现的接口中含有相同的方法

    首先,说一下,当某一个类实现了两个接口的时候,两个接口中存在两个相同的方法,在实现的类中只需实现一个方法的方法体. 当一个类继承一个类,并且实现一个或者多个接口的时候,其中,父类和父接口中存在相同的方 ...

  7. Boost::filesystem 使用小笔记

    今天拿起手要用C++写个小工具,从指定的目录递归遍历文件,然后做一下处理.又翻了一下boost的filesystem库.小结一下,希望能加深印象,免得下次又要查看文档. 1. path对象就是一个跨平 ...

  8. Oracle查看表空间使用情况

     查看表空间使用情况 select upper(f.tablespace_name) "表空间名",        d.tot_grootte_mb "表空间大小(m ...

  9. 浅谈PPM (Project Portfolio Management) - 1

    前言: 本文以纯理论性的PPM解说为主,不会涉及到具体怎样实施,我会在以后介绍具体的PPM实施方案介绍. PPM,可能非常多人并不清楚甚至可能没听说过,这是一个近些年才流行起来的概念,是Project ...

  10. while和do while习题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习 { ...