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. css3 box-sizing属性值详解

    box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...

  2. jvisualvm 远程连接jboss

    由于项目中使用jboss 作为web容器,每当项目上线时需要使用loadrunner对项目进行性能压测,这时就需要实时观察JVM的一些参数.想使用jvisualvm借助jstatd远程连接服务器上面的 ...

  3. 【LOJ】#2017. 「SCOI2016」围棋

    题解 考虑到状态数比较复杂,其实我们需要轮廓线dp-- 我们设置\(f[x][y][S][h][k]\)为考虑到第(x,y)个格子,S是轮廓线上的匹配状态,是二进制,如果一位是1表示这一位匹配第一行匹 ...

  4. bzoj 1452 二维树状数组

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  5. 久邦数码(3G门户)面试

    久邦数码(3G门户)面试 1.数组和链表的区别(为什么数组带有索引) 2.数据库(手写选出一个公司年龄最大的100个员工) 3.一百个数查找一个数 利用二分查找一个数在最差的情况下至少比较多少次 4. ...

  6. [Codeforces995C]Leaving the Bar 瞎搞

    大致题意: 给出平面上n个向量,对于每个向量可以选择正的V或负的-V,求按照选择的向量走完,最后距离原点<=1.5*1e6的一个选择方案 非正解!!!!!!!!!! 先按距离原点距离由远到近贪心 ...

  7. 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?

    题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...

  8. Spring boot的hot swapping

    前言 嘛,都是看官方文档的,就先贴上文档地址: using-boot-hot-swapping 使用 使用hot swapping只需要把devtools的jar包添加到你的classpath里. 在 ...

  9. hashMap原理剖析

    在日常开发中,hashMap应该算是比较常用的一个类了,今天就来学习一下hashMap的实现原理. 概念 1.什么时hash? 书面定义:就是把一个不固定长度的二进制值映射成一个固定长度的二进制值. ...

  10. NOIP2017 D1T1小凯的疑惑

    这应该是近年来最坑的第一题了. 我第一眼看到这题上来就打表,数据范围告诉我复杂度应该是log级的,然而一个小时后才发现是一个输出结论. 设较小数是a 较大数是b 写出几组可以发现一个规律就是一旦出现连 ...