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. review-反思当程序猿的小一年来

    误打误撞进入这个行业,也算是缘分把,不到一年的时光里,剖析一下自己,别写了半天代码,学了一堆东西,不知道干嘛.反省一下. 1.目标与知识库 就目前在我看来,是想成为一名优秀的数据工程师,掌握全栈数据分 ...

  2. 《Android虚拟机》----虚拟机概述

    No1: 虚拟机是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离的环境中的完整计算机系统. No2: Java虚拟机由如下五个部分组成:一组指令集.一组寄存器.一个栈.一个无用单元收集堆. ...

  3. 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array

    题目描述 Given an array A with length n  a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...

  4. 邝斌带你飞之数论专题--Maximum GCD UVA - 11827

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...

  5. I/O 多路复用之select、poll、epoll详解

    select,poll,epoll都是IO多路复用的机制.I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但s ...

  6. Java并发(八):AbstractQueuedSynchronizer

    先做总结: 1.AbstractQueuedSynchronizer是什么? AbstractQueuedSynchronizer(AQS)这个抽象类,是Java并发包 java.util.concu ...

  7. 没有调用PageHelper.startPage()分页方法,最后还是执行了PageHelper分页方法的原因

    SELECT * FROM ( SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( SELECT * FROM ( SELECT A.*, ROWNUM RN FROM ( ...

  8. 三周学会小程序第四讲:Heroku 绑定 Github 自动部署

    这一讲是根据读者的反馈补充的一个讲解,好多读者反应安装 Heroku-cli 遇到问题,或者是操作繁琐,其实上一讲中提到的 Heroku 只是为了免费部署,而安装 Heroku-CLI只是为了部署,所 ...

  9. 让你的chrome开发工具console支持jquery

    首先执行以下代码: ;(function(d,s){d.body.appendChild(s=d.createElement('script')).src='http://code.jquery.co ...

  10. bzoj 3289 莫队 逆序对

    莫队维护逆序对,区间左右增减要分类讨论. 记得离散化. /************************************************************** Problem: ...