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

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

 
 
题目意思:
给出两个四位数的素数,要求对第一个素数进行一系列操作,使他变成第二个四位数的素数,每次操作只能改变一位数,且要求每次操作之后的数仍然是素数
问你最少的操作步骤数是多少?
 
分析:
先对四位数的素数打一个表
然后bfs
需要注意的是每次搜索入队之后,要进行回退操作
注意设置标记位(该数已经搜索过)
 
code:
  1. #include<stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <math.h>
  7. #include <cstdlib>
  8. #include <queue>
  9. using namespace std;
  10. #define max_v 10100
  11. int prime[max_v];
  12. int vis[max_v];
  13. struct node
  14. {
  15. int x,step;
  16. };
  17. void init()//打N以内的素数表,prime[i]=0为素数。
  18. {
  19. memset(prime,,sizeof(prime));
  20. prime[]=;
  21. for(int i=; i<max_v; i++)
  22. {
  23. if(prime[i]==)
  24. {
  25. for(int j=; j*i<max_v; j++)
  26. prime[j*i]=;
  27. }
  28. }
  29. }
  30. int bfs(int s,int e)
  31. {
  32. int num;
  33. memset(vis,,sizeof(vis));//vis[N]用来标记是否查找过
  34. queue<node> q;
  35.  
  36. node p,next;
  37.  
  38. p.x=s;
  39. p.step=;
  40.  
  41. vis[s]=;
  42. q.push(p);
  43.  
  44. while(!q.empty())
  45. {
  46. p=q.front();
  47. q.pop();
  48.  
  49. if(p.x==e)
  50. {
  51. return p.step;
  52. }
  53. int t[];
  54. t[]=p.x/;//千
  55. t[]=p.x/%;//百
  56. t[]=p.x/%;//十
  57. t[]=p.x%;//个
  58.  
  59. for(int i=; i<=; i++)
  60. {
  61. int temp=t[i];//这里特别注意,每次只能变换一位数,这里用来保存该变换位的数,变换下一位数时,该位数要恢复
  62. for(int j=; j<; j++)
  63. {
  64. if(t[i]!=j)
  65. {
  66. t[i]=j;//换数
  67. num=t[]*+t[]*+t[]*+t[];
  68. }
  69. if(num>=&&num<=&&vis[num]==&&prime[num]==)//满足要求,四位数,没有用过,是素数
  70. {
  71. next.x=num;
  72. next.step=p.step+;
  73. q.push(next);
  74. vis[num]=;
  75. }
  76. }
  77. t[i]=temp;//恢复
  78. }
  79. }
  80. return -;
  81. }
  82. int main()
  83. {
  84. //题意:给你两个4位数,要求你每次只能变换一位数字,并且变换前后的数字都要满足是素数;求最少变换次数;
  85. //首先打好素数表,再进行BFS
  86. int n,a,b,ans;
  87. init();
  88. cin>>n;
  89. while(n--)
  90. {
  91. cin>>a>>b;
  92. ans=bfs(a,b);
  93. if(ans==-)
  94. {
  95. cout<<"Impossible"<<endl;
  96. }
  97. else
  98. {
  99. cout<<ans<<endl;
  100. }
  101. }
  102. return ;
  103. }

POJ 3216 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. POJ 3126 - Prime Path - [线性筛+BFS]

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

  3. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

  4. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  5. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

  6. 双向广搜 POJ 3126 Prime Path

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

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

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

  8. poj 3126 Prime Path bfs

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

  9. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

随机推荐

  1. easyui前后台转义字符和普通字符的相互转换问题

    昨天碰到一个问题,公司前端使用的是easyui和jquery,页面textarea编写了html代码,传入后台变成了<>类型代码,这样保存到数据库是没有问题的,但是在页面显示的时候需要显示 ...

  2. jvm内置锁synchronized不能被中断

    很久没看技术书籍了,今天看了一下<七周七并发模型>前面两章讲的java,写的还是有深度的.看到了一个有demo,说jvm内置锁synchronized是不能被中断的.照着书上写了个demo ...

  3. csharp: QR Code Barcode

    /// <summary> /// /// </summary> /// <param name="sender"></param> ...

  4. Java 使用Log4J进行日志操作

    使用Log4J进行日志操作   Log4J简介   Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT ...

  5. css伪元素详解

    css的伪元素,之所以被称为伪元素,是因为他们不是真正的页面元素,html没有对应的元素,但是其所有用法和表现行为与真正的页面元素一样,可以对其使用诸如页面元素一样的css样式,表面上看上去貌似是页面 ...

  6. SpringBoot - Starter

    If you work in a company that develops shared libraries, or if you work on an open-source or commerc ...

  7. WinAPI: OpenProcess、GetExitCodeProcess、TerminateProcess (测试强制关闭 OICQ)

    原文:http://www.cnblogs.com/del/archive/2008/03/10/1098502.html //声明: {返回进程的句柄} OpenProcess(   dwDesir ...

  8. 千里之堤毁于蚁穴(慎用HD Wallets)

    转自:http://blog.sina.com.cn/s/blog_12ce70a430102vbu9.html 千里之堤毁于蚁穴(慎用HD Wallets) -- 随机系列谈之四 现在我们都该明白, ...

  9. leetcode-Restore IP Addresses-ZZ

    http://www.cnblogs.com/remlostime/archive/2012/11/14/2770072.html class Solution { private: vector&l ...

  10. 鲁棒图(Robustness Diagram)

    鲁棒图与系统需求分析 鲁棒图(Robustness Diagram)是由Ivar Jacobson于1991年发明的,用以回答“每个用例需要哪些对象”的问题.后来的UML并没有将鲁棒图列入UML标准, ...