Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9519   Accepted: 5458

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
    题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素数;
  4.  
  5. 思路:将四位数以内的素数筛选出来,bfs时,枚举四位数的每一位上的每一个数;
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. int num;
  9. int step;
  10. };
  11. queue<struct node>que;
  12. int n,m,flag;
  13. bool p[],vis[];
  14.  
  15. //素数筛;
  16. void prime_search()
  17. {
  18. memset(p,,sizeof(p));
  19. for(int i = ; i <= ; i+=)
  20. p[i] = ;
  21. for(int i = ; i <= ; i++)
  22. {
  23. if(p[i])
  24. {
  25. for(int j = i+i; j <= ; j += i)
  26. p[j] = ;
  27. }
  28. }
  29. }
  30.  
  31. int bfs()
  32. {
  33. while(!que.empty())
  34. que.pop();
  35. que.push((struct node){n,});
  36. vis[n] = ;
  37. int res,tmp,r,t,s;
  38. while(!que.empty())
  39. {
  40. struct node u = que.front();
  41. que.pop();
  42. if(u.num == m)
  43. return u.step;
  44.  
  45. //枚举个位数
  46. r = u.num%;
  47. for(int k = -; k <= ; k++)
  48. {
  49. t = r+k;
  50. if(t >= && t <= )
  51. {
  52. res = (u.num/)*+t;
  53. if(p[res] && !vis[res])
  54. {
  55. que.push((struct node){res,u.step+});
  56. vis[res] = ;
  57. }
  58. }
  59. }
  60.  
  61. //枚举十位数
  62. tmp = u.num/;
  63. r = tmp%;
  64. s = tmp/;
  65. for(int k = -; k <= ; k++)
  66. {
  67. t = r+k;
  68. if(t >= && t <= )
  69. {
  70. res = (s*+t)*+u.num%;
  71. if(p[res] && !vis[res])
  72. {
  73. que.push((struct node){res,u.step+});
  74. vis[res] = ;
  75. }
  76. }
  77. }
  78.  
  79. //枚举百位数
  80. int tmp = u.num/;
  81. r = tmp%;
  82. s = tmp/;
  83. for(int k = -; k <= ; k++)
  84. {
  85. t = r+k;
  86. if(t >= && t <= )
  87. {
  88. res = (s*+t)*+u.num%;
  89. if(p[res] && !vis[res])
  90. {
  91. que.push((struct node){res,u.step+});
  92. vis[res] = ;
  93. }
  94. }
  95. }
  96.  
  97. //枚举千位数
  98. r = u.num/;
  99. for(int k = -; k <= ; k++)
  100. {
  101. t = r+k;
  102. if(t > && t <= )
  103. {
  104. res = t*+u.num%;
  105. if(p[res] && !vis[res])
  106. {
  107. que.push((struct node){res,u.step+});
  108. vis[res] = ;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. int main()
  115. {
  116. int test;
  117. prime_search();
  118. scanf("%d",&test);
  119. while(test--)
  120. {
  121. memset(vis,,sizeof(vis));
  122. scanf("%d %d",&n,&m);
  123. int ans = bfs();
  124. printf("%d\n",ans);
  125. }
  126. return ;
  127. }

Prime Path(素数筛选+bfs)的更多相关文章

  1. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  2. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  3. poj3126 Prime Path 广搜bfs

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

  4. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  5. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  6. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  7. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

  8. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  9. Prime Path[POJ3126] [SPFA/BFS]

    描述 孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了.zydsg要做些改变,他想去和素数小姐姐约会. 所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样 ...

随机推荐

  1. 亲测apache

    http://www.cnblogs.com/bluewelkin/p/3805107.html 里面是纠正了原文的一些小错误,即可正常安装  1.su 命令 2.安装apr-1.3.5.tar.gz ...

  2. 高效 css 整理

    避免通用规则 请确保规则不以通用类型作为结束! 不要用标签名或 classes 来限制 ID 规则 如果规则的关键选择器为 ID 选择器,则没有必要为规则增加标签名.因为 ID 是唯一的,增加标签只会 ...

  3. PHP利用超级全局变量$_POST来接收表单数据。

    利用$_POST超级全局变量接收表单的数据,然后利用echo输出到页面. 下面是代码: <!doctype html> <html> <head> <titl ...

  4. rabbitmq 消息持久化之receive and send

    二: 任务分发 &消息持久化   启用多个接收端的时候如果某一个receive 关闭要保证消息有反馈是否收到   send端 #-*- coding: UTF-8 -*-import pika ...

  5. css 实现页面加载中等待效果

    <!DOCTYPE html> <html> <head> <title>css实现页面加载中,请稍候效果</title> <meta ...

  6. js中浮点型运算 注意点

    先看张图: 这是一个JS浮点数运算Bug,导致我树状图,数据合计不正确,,,,,,两个小数相加,出来那么多位小数 (这是修该之后的) 网上找到以下解决方式: 方法一:有js自定义函数   <sc ...

  7. 函数对象的prototype总结

    通过看 http://www.cnblogs.com/mindsbook/archive/2009/09/19/javascriptYouMustKnowPrototype.html 该文章和对代码的 ...

  8. c# 连接oracle 读取数据

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 用html/css做的一个登入小界面(图片瀑布流)

    一个登入效果简易图:(色彩搭配有点乱,嘻嘻,可以在代码处改成自己喜欢的颜色) css样式的代码: style.css: @charset "utf-8";/* CSS Docume ...

  10. C++ cout cerr 和 clog 的区别

    我们都知道C++预定义了cin(标准输入流)和cout(标准输出流).但今天突然又蹦出来两个cerr(标准错误流(非缓冲))和clog(标准错误流(缓冲)),本着学习提高的态度在网上搜索了相关内容,下 ...