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.

  1. 1033
  2. 1733
  3. 3733
  4. 3739
  5. 3779
  6. 8779
  7. 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 Sample Output 1
  1. 3
  2. 1033 8179
  3. 1373 8017
  4. 1033 1033
  1. 6
  2. 7
  3. 0

大致意思就是由前面的那个素数变到后面的那个素数,每次只能变一位数,变化后的数也应该是一个素数(无论是不是所求的数),求变化次数。

直接BFS即可。

  1. //Asimple
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. const int maxn = ;
  6. ll n, m, s, res, ans, len, T, k;
  7. int x, y;
  8. int pr[maxn];
  9.  
  10. int P(int n) {
  11. for(int i=; i*i<=n; i++) {
  12. if( n%i== ) return ;
  13. }
  14. return ;
  15. }
  16. //将k位化0
  17. int change(int n, int k) {
  18. char s[] = {};
  19. sprintf(s, "%d", n);
  20. s[k] = '';
  21. sscanf(s, "%d", &n);
  22. return n;
  23. }
  24.  
  25. int solve(int s, int e) {
  26. queue<int> q;
  27. int dis[maxn] = {};
  28. q.push(s);
  29. dis[s] = ;
  30. while( q.size() ) {
  31. s = q.front();
  32. q.pop();
  33. if( s == e ) return dis[s]-;
  34. int t = ;
  35. for(int i=; i<; i++) {
  36. int k = change(s, i);
  37. for(int j=; j<; j++) {
  38. int a = k+j*t;
  39. if( pr[a]== && dis[a]== ) {
  40. q.push(a);
  41. dis[a] = dis[s]+;
  42. }
  43. }
  44. t /= ;
  45. }
  46. }
  47. return -;
  48. }
  49.  
  50. void input() {
  51. for(int i=; i<maxn; i++) pr[i] = P(i);
  52. cin >> T;
  53. while( T -- ) {
  54. cin >> x >> y;
  55. ans = solve(x, y);
  56. if( ans==- ) puts("Impossible");
  57. else cout << ans << endl;
  58. }
  59. }
  60.  
  61. int main(){
  62. input();
  63. return ;
  64. }

Kattis之旅——Prime Path的更多相关文章

  1. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  2. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  3. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. POJ2126——Prime Path(BFS)

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

  6. Prime Path(poj 3126)

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

  7. Prime Path(素数筛选+bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9519   Accepted: 5458 Description The m ...

  8. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  9. Prime Path(POJ 3126 BFS)

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

随机推荐

  1. cordava打包vue项目成app

    注意:安装目录不要以中文命名 1.安装cordova :npm install -g cordova 2.安装java jdk :配置环境变量: 1.系统变量:名:JAVA_HOME    值:C:\ ...

  2. [js]纯css强制不换行

    要加在li上 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  3. (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘

    什么是以太坊(Ethereum) 以太坊(Ethereum)是一个基于区块链技术,允许任何人构建和使用去中心化应用的区块链平台.像比特币一样,以太坊是开源的,并由来自全世界的支持者们共同维护.与比特币 ...

  4. 【UML】-NO.42.EBook.5.UML.1.002-【UML 大战需求分析】- 活动图 (Activity Diagram)

    1.0.0 Summary Tittle:[UML]-NO.42.EBook.1.UML.1.002-[UML 大战需求分析]- 活动图 Style:DesignPattern Series:Desi ...

  5. FreeMaker常用表达式

    1,日期转换 ${data.startDate?string("yyyy-MM-dd HH:mm")} 2,非空检查 当数据为null时,1中日期转换在界面上不会显示异常,但在日志 ...

  6. zabbix 自定义监控文本内容

    需求:监控服务器硬盘使用率是否有超过80%的 需要监控的文本 root@zabbix zabbix]# cat /etc/zabbix/scripts/data/monitor_disk.txt &q ...

  7. 【Redis】主从同步

    Redis提供了主从复制功能,主要是为了保证服务的高可用性.在redis.conf配置文件中通过设置,可以开启主从复制功能.或者在客户端中使用slaveof 命令开启该功能. slaveof < ...

  8. 从零开始一起学习SLAM | 三维空间刚体的旋转

    刚体,顾名思义,是指本身不会在运动过程中产生形变的物体,如相机的运动就是刚体运动,运动过程中同一个向量的长度和夹角都不会发生变化.刚体变换也称为欧式变换. 视觉SLAM中使用的相机就是典型的刚体,相机 ...

  9. 利用css伪类编写冒泡小三角

    HTML代码 <div class="lf otherLogin"> <span>其他方式注册</span> <div class=&qu ...

  10. oracle中实现自增id

    在一些数据库(例如mysql)中,实现自增id只要在建表的时候指定一下即可, 但是在oracle中要借助sequence来实现自增id, 要用上自增id,有几种方式: 1.直接在insert语句中使用 ...