Prime Path
 

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 题意 将a转换到b,每次换一位,每次交换后的数都是素数,算最短换了多少次
我的思路
找到所有的四位数的素数,打表
用bfs 有四十个方向,找到答案
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int prime[],n,m,tail,head;
struct que
{
int x,time;
}que[];
int row(int x)
{
int i,s=;
for(i=;i<=x;i++)
s*=;
return s;
}
int bfs()
{
int i,j,x;
for(i=;i<=;i++)
{
for(j=;j<=i/;j++)
if(i%j==)break;
if(j==i/+)prime[i]=;
else prime[i]=;
}
prime[n]=;
tail=head=;
que[].x=n,que[].time=;
while(head<=tail)
{
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
x=que[head].x-(que[head].x/row(i-)%)*row(i-)+j*row(i-);
if(x>=&&x<=&&prime[x])
{
que[++tail].x=x;
que[tail].time=que[head].time+;
if(x==m)return que[tail].time;
else prime[x]=;
}
}
}
head++;
}
return ;
}
int main()
{
int i,j,key,g;
while(scanf("%d",&g)==)
{
for(i=;i<g;i++)
{
scanf("%d %d",&n,&m);
if(n==m)
{
printf("0\n");
continue;
}
key=bfs();
if(key)
printf("%d\n",key);
else
printf("Impossible\n");
}
}
return ;
}
												

poj3126 Prime Path(c语言)的更多相关文章

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

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

  2. poj3126 Prime Path 广搜bfs

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

  3. POJ3126 Prime Path —— BFS + 素数表

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

  4. POJ3126 Prime Path

    http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...

  5. POJ3126 Prime Path(BFS)

    题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  6. POJ3126——Prime Path

    非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...

  7. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  8. Prime Path POJ-3126

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

  9. POJ2126——Prime Path(BFS)

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

随机推荐

  1. <!DOCTYPE>标签与table高度100% (转)

    <!DOCTYPE>标签可声明三种DTD类型,分别表示严格版本.过渡版本以及基于框架的 HTML 文档. 三种HTML文档类型: HTML 4.01 规定了三种文档类型:Strict.Tr ...

  2. python免费发送短信

    pip install twilio from twilio.rest import Client # Your Account SID from twilio.com/console account ...

  3. St_geometry 初始用

    数据准备 点表 CREATE TABLE point_stgeom tablespace UBOSS_STS_WAREHOUSE as select car_number, longid, latid ...

  4. Android——Intent和Intent过滤器

    http://www.cnblogs.com/XP-Lee/p/3613830.html Intent就是一个激活组件的消息对象,用于组件之间的通信.需要注意的是,能被Intent激活通信的组件只有三 ...

  5. console.time和console.timeEnd用法

    console.time和console.timeEnd这两个方法可以用来让WEB开发人员测量一个javascript脚本程序执行消耗的时间.随着WEB应用越来越重要,JavaScript的执行性能也 ...

  6. LCA转换成RMQ

    LCA(Lowest Common Ancestor 最近公共祖先)定义如下:在一棵树中两个节点的LCA为这两个节点所有的公共祖先中深度最大的节点. 比如这棵树 结点5和6的LCA是2,12和7的LC ...

  7. 转载 [深入学习C#]C#实现多线程的方式:使用Parallel类

    简介 在C#中实现多线程的另一个方式是使用Parallel类. 在.NET4中 ,另一个新增的抽象线程是Parallel类 .这个类定义了并行的for和foreach的 静态方法.在为 for和 fo ...

  8. mysql order by 排序的问题

    参考博客http://blog.csdn.net/hollboy/article/details/13296601 mysql order by 的排序在今天时候遇到了问题 情景是:将排序的字段设置成 ...

  9. [转]qtcreator中常用快捷键总结

    F1 查看帮助 F2 跳转到函数定义(和Ctrl+鼠标左键一样的效果) Shift+F2 声明和定义之间切换 F4 头文件和源文件之间切换 Ctrl+ 欢迎模式 Ctrl+ 编辑模式 Ctrl+ 调试 ...

  10. centos7搭建logstash

    前两节已经成功完成ek的搭建,还剩最后的一个日志上传的功能 依次执行如下命令 cd /home/elk wget https://artifacts.elastic.co/downloads/logs ...