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 题意:给你两个四位数m和n,求m变到n至少需要几步;每次只能从个十百千上改变一位数字,并且改变后的数要是素数;
我们可以用优先对列做;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#define N 11000
using namespace std;
int prime(int n)
{
int i,k;
k=(int)sqrt(n);
for(i=;i<=k;i++)
if(n%i==)
return ;
return ;
}
struct node
{
int x,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
int bfs(int m,int n)
{
int a[]={,,,};
int vis[N];
priority_queue<node>Q;
node q,p;
memset(vis,,sizeof(vis));
vis[m]=;
q.x=m;
q.step=;
Q.push(q);
while(!Q.empty())
{
q=Q.top();
Q.pop();
if(q.x==n)
return q.step;
for(int i=;i<;i++)//控制改变的哪一位
{
for(int j=;j<;j++)//把相对应的那一位变成j;
{
int L=q.x/(a[i]*);
int R=q.x%(a[i]);
p.x=L*(a[i]*)+j*a[i]+R;//重新组成的数;
if(p.x>=&&vis[p.x]==&&prime(p.x)==)
{
vis[p.x]=;
p.step=q.step+;
Q.push(p);
}
}
}
}
return -;
} int main()
{
int T,m,n,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
ans=bfs(m,n);
if(ans==-)
printf("Impossible\n");
else
printf("%d\n",ans);
}
return ;
}

Prime Path--POJ3126(bfs)的更多相关文章

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

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

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

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

  3. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  4. (简单) POJ 3126 Prime Path,BFS。

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

  5. HDU - 1973 - Prime Path (BFS)

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

  6. POJ - 3126 - Prime Path(BFS)

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

  7. Prime Path POJ-3126

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

  8. poj3216 Prime Path(BFS)

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

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

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

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

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

随机推荐

  1. 执行RF设置顶级测试套件的名称

    场景1:通过pybot进行单个output文件情况下设置 -N --name name 设置顶级测试套件的名称.名称中的下划线将转换为空格. 默认名称为执行的数据源的名称. 场景2:通过rebot进行 ...

  2. Splash plugins_enabled 属性

    plugins_enabled属性可以控制浏览器插件(如 Flash 插件)是否开启.默认情况下,此属性是 false ,表示不开启. function main(splash, args) spla ...

  3. mkubimage-mlc2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: No such file or directory

    mkubimage-mlc2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: ...

  4. [Cubieboard] 在 Cubieboard 上安装 Node.js 和 npm

    你有两个选择可以实现在Cubieboard上安装NodeJS,下载别人已经编译完成适用于Cubieboard的NodeJS二进制包,或者自己下载源码自行在Cubieboard上进行编译. 使用编译完成 ...

  5. Linux 下 c 语言 聊天软件

    这是我学C语言写的第一个软件,是一个完整的聊天软件,里面包括客户端,和服务器端,可以互现聊天,共享文件,有聊天室等,是一个有TCP和UDP协议的聊天软件,测试过很多次在CENTOS和UBUNTU下都通 ...

  6. fastcgi协议之一:定义

    参考 深入理解fastcgi协议以及在php中的实现 https://mengkang.net/668.html fastcgi协议规范内容 http://andylin02.iteye.com/bl ...

  7. 剑指offer面试题7:用两个栈实现队列

    题目1:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 代码实现: public class Solution07 { Stack<Integer> ...

  8. RabbitMQ备份交换器

    备份交换器,AlternateExchange(AE): 备份交换器是为了实现没有路由到队列的消息,与上篇介绍到的mandatory都是为了处理没有路由到的消息. AE相对于mandatory逻辑更简 ...

  9. Android中Adapter总结

    根据一个制作列表的程序开始练手,结果就出现了学习安卓的第一个代码问题 运行程序发现,虽然报错,但是可以成功运行程序. Android中Adapter功能为 显示ListView,最常用的有ArrayA ...

  10. ldap 测试表设计

    1. ldap_oc_mappings    存储objeckClass 信息 表结构:  Column Desc. id objectClass的唯一标识 name objectClass的名称 k ...