Prime Path

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 18
Problem 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
3
1033 8179
1373 8017
1033 1033
 
Sample Output
6
7
0
 
Source
PKU
 
题意:
给出两个四位的素数,要求求出从其中一个变化到另一个数的最少的变化次数,每一次变化只变化四位中的一位,并且变化后的数也要是素数;
思路:
bfs,只不过是40入口的bfs,需要经过剪枝;每一次都枚举个位、十位、百位、千位的所有变化,检验室素数后加入到队列中;
AC代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6. int a,b;
  7. struct kf
  8. {
  9. int number;
  10. int sgin;
  11. }ks[];
  12. bool ksgin[]={false};
  13.  
  14. bool shu(int sg)//判断sg是否是素数
  15. {
  16. if(sg==||sg==)
  17. return true;
  18. else if(sg<=||sg%==)
  19. return false;
  20. else if(sg>)
  21. {
  22. for(int i=;i*i<=sg;i+=)
  23. if(sg%i==)
  24. return false;
  25. return true;
  26. }
  27. }
  28.  
  29. int bfs()
  30. {
  31. int left,right;
  32. kf s;
  33. ks[left=right=].number=a;
  34. ks[right++].sgin=;
  35. ksgin[a]=false;
  36. while(left<right){
  37. s=ks[left++];
  38. if(s.number==b){
  39. cout<<s.sgin<<endl;
  40. return ;
  41. }
  42. int ge=s.number%;
  43. int shi=(s.number/)%;
  44. for(int i=;i<=;i+=){//枚举个位
  45. int y=s.number/*+i;
  46. if(y!=s.number&&ksgin[y]&&shu(y)){
  47. ksgin[y]=false;
  48. ks[right].number=y;
  49. ks[right++].sgin=s.sgin+;
  50. }
  51. }
  52. for(int i=;i<=;i++){//枚举十位
  53. int y=s.number/*+i*+ge;
  54. if(y!=s.number&&ksgin[y]&&shu(y)){
  55. ksgin[y]=false;
  56. ks[right].number=y;
  57. ks[right++].sgin=s.sgin+;
  58. }
  59. }
  60. shi*=;
  61. shi+=ge;
  62. for(int i=;i<=;i++){//枚举百位
  63. int y=s.number/*+i*+shi;
  64. if(y!=s.number&&ksgin[y]&&shu(y)){
  65. ksgin[y]=false;
  66. ks[right].number=y;
  67. ks[right++].sgin=s.sgin+;
  68. }
  69. }
  70. shi=s.number%;
  71. for(int i=;i<=;i++){//千位
  72. int y=i*+shi;
  73. if(y!=s.number&&ksgin[y]&&shu(y)){
  74. ksgin[y]=false;
  75. ks[right].number=y;
  76. ks[right++].sgin=s.sgin+;
  77. }
  78. }
  79. }
  80. cout<<"Impossible"<<endl;
  81. return ;
  82. }
  83.  
  84. int main()
  85. {
  86. // freopen("input.txt","r",stdin);
  87. int test;
  88. cin>>test;
  89. while(test--){
  90. memset(ksgin,true,sizeof(ksgin));
  91. cin>>a>>b;
  92. bfs();
  93. }
  94. }

Prime Path(BFS)的更多相关文章

  1. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  3. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  4. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  5. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

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

  8. POJ - 3126 - Prime Path(BFS)

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

  9. POJ-3126-Prime Path(BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27852   Accepted: 15204 Desc ...

随机推荐

  1. PhpMyAdmin隐藏数据库设置同前缀失效的问题

    用PhpMyAdmin默认会把所有数据库都显示出来,一些如 MySQL,information_schema之类的也会显示,这样既不安全看着也不爽,隐藏掉最好. 修改 config.inc.php 或 ...

  2. CodeForces 707A Brain's Photos

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  3. 史上最详细SharePoint 2013安装步骤图解新手教程

    来源:// http://www.itexamprep.com/cn/microsoft/soft/sharepoint2013/2013/0408/2866.html 文章就是SharePoint2 ...

  4. ubuntu开放防火墙端口

    root@jbxue:$ sudo ufw enable  Firewall started and enabled on system startup  root@jbxue:$ sudo ufw ...

  5. java中equals方法和contentEquals方法区别

    java中,String类里提供了两种字符串的比较方式(算上“==”应该是三种) String line1 = new String("0123456789"); String l ...

  6. java 学习心得

  7. SQL 小笔记

    如何得到字段的类型 select sql_variant_property(ID,'BaseType') from tb

  8. centos7,yum安装的redis用systemctl无法启动

    因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...

  9. one-sided limit

    Limit[e^(-1/x),x->0,Direction->-1] means $\lim_{x \to 0^{+}}e^{-\frac{1}{x}}$ Limit[e^(-1/x),x ...

  10. 表格单元格td设置宽度无效的解决办法 .

    http://zzstudy.offcn.com/archives/11366 在做table页面时,有时对td设置的宽度是无效的,td的宽度始终有内部的内容撑开,可以设置padding,但直接设置w ...