Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11751   Accepted: 6673

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

给出两个数s,e,都是素数。经过转化,将s转化为e的最小步数

规则,每一次仅仅能改动一位,每次得到的数都是素数。

素数筛跑出1000到10000内的全部素数。假设当中两个素数仅仅有一位不同。那么连接一条边。得到全部素数组合的图后用bfs直接搜索就能够

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int a[11000] , check[11000] , tot ;
struct node{
int v , next ;
}p[2000000];
struct node1{
int u , t ;
};
queue <node1> que ;
int head[10000] , cnt , flag[10000] ;
void add(int u,int v)
{
p[cnt].v = v ;
p[cnt].next = head[u] ;
head[u] = cnt++ ;
}
void init()
{
memset(check,0,sizeof(check));
memset(head,-1,sizeof(head));
tot = cnt = 0 ;
int i , j , k , num ;
for(i = 2 ; i <= 10000 ; i++)
{
if( !check[i] )
a[tot++] = i ;
for(j = 0 ; j < tot ; j++)
{
if(i*a[j] >= 10000)
break;
check[i*a[j]] = 1 ;
if( i%a[j] == 0 )
break;
}
}
for(i = 0 ; i < tot ; i++)
if( (a[i]/1000) ) break;
k = i ;
for(i = k ; i < tot ; i++)
{
for(j = k ; j < i ; j++)
{
num = 0 ;
if( a[i]%10 != a[j]%10 )
num++ ;
if( a[i]/10%10 != a[j]/10%10 )
num++ ;
if( a[i]/100%10 != a[j]/100%10 )
num++ ;
if( a[i]/1000%10 != a[j]/1000%10 )
num++ ;
if(num == 1)
{
add(i,j);
add(j,i);
}
}
}
}
int find1(int x)
{
int low = 0 , mid , high = tot-1 ;
while(low <= high)
{
mid = (low+high)/2 ;
if(a[mid] == x)
return mid ;
else if(a[mid] < x)
low = mid + 1 ;
else
high = mid -1 ;
}
}
int bfs(int s,int e)
{
memset(flag,0,sizeof(flag));
while( !que.empty() )
que.pop();
int i , j , v ;
node1 low , high ;
low.u = s ;
low.t = 0 ;
flag[s] = 1 ;
que.push(low);
while( !que.empty() )
{
low = que.front();
que.pop();
if( low.u == e )
return low.t ;
for(i = head[low.u] ; i != -1 ; i = p[i].next)
{
v = p[i].v ;
if( !flag[v] )
{
flag[v] = 1;
high.u = v ;
high.t = low.t + 1 ;
que.push(high);
}
}
}
return 0;
}
int main()
{
int t , s , e ;
init();
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &s, &e);
s = find1(s);
e = find1(e);
printf("%d\n", bfs(s,e) );
}
return 0;
}

poj3126--Prime Path(广搜)的更多相关文章

  1. poj3126 Prime Path 广搜bfs

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

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

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

  3. POJ3126——Prime Path

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

  4. poj3126 Prime Path(c语言)

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

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

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

  6. POJ3126 Prime Path

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

  7. POJ3126 Prime Path(BFS)

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

  8. 双向广搜 POJ 3126 Prime Path

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

  9. POJ 3126 Prime Path 简单广搜(BFS)

    题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...

随机推荐

  1. MySQL之正则表达式

    一.介绍 正则表达式用来描述或者匹配符合规则的字符串.它的用法和like比较相似,但是它又比like更强大,能够实现一些很特殊的规则匹配:正则表达式需要使用REGEXP命令,匹配上返回"1& ...

  2. IEEEXtreme 10.0 - Mancala'h

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mancala'h 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  3. hdu4796

    4月真是没写啥题,这题还是月初写的…… 不错的插头dp,首先由n和m的范围知肯定是轮廓线是横向划的 问题的难点在于怎么处理下面两个问题 1.怎么处理独立插头 2.怎么处理完全将W和L左右隔开 先说独立 ...

  4. 小甲鱼C++笔记(上)1-24

    一 OO思想:每个对象都是一个完整的独立的个体,由相关的属性和行为组合与外界分隔 OO思想的特点:1封装 把对象的属性和方法结合成一个独立的系统单位,并尽可能隐藏内部细节 2抽象 对一类公共问题进行统 ...

  5. python3环境下面bytes类型转换成字典类型实例

    场景:通过http://tool.chinaz.com/tools/httptest.aspx在线HTTP接口测试工具获取接口的返回信息 { "status": 0, " ...

  6. Python并发编程-守护进程

    守护进程 子进程转换为守护进程 主进程的代码结束,子进程的代码也应该接收, 这个事情有守护进程来做 守护进程会随着主进程的代码执行完毕而结束, 而不是随着主进程的接收而结束(子进程不一定结束) fro ...

  7. Python类总结-字段,方法,属性区别及StaticMethod, Property,私有字段和私有属性

    类包含下列 静态属性 动态属性 静态方法 动态方法 class Province: #静态字段--属于类,调用方法类.字段名 memo = "中国23个省之一" #动态字段--属于 ...

  8. Python之路【第三篇】:文件操作

    一.文件操作步骤 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 歌名:<大火> 演唱:李佳薇 作词:姚若龙 作曲:马奕强 歌词: 有座巨大的停了的时钟 倾倒在赶 ...

  9. ssm demo,用户角色权限管理

    SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...

  10. java反射,代码优化

    java的反射机制属实强大,能解决好些问题 在接手别人写的代码的时候,有一个bean类的get方法特别low,我都看不下去 重复代码写五遍,我都觉得太不合理.之后将其中代码抽取出来修改了下. publ ...