Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9982   Accepted: 5724

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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff; //无限大
const int MAXN = ;
bool flag[MAXN];
int primes[MAXN], pi;
struct point
{
int x;
int y;
};
void GetPrime_1()
{
int i, j;
pi = ;
memset(flag, false, sizeof(flag));
for (i = ; i < MAXN; i++)
if (!flag[i])
{
primes[i] = ;//素数标识为1
for (j = i; j < MAXN; j += i)
flag[j] = true;
}
}
int vis[maxn];
int main()
{
GetPrime_1();
int t;
cin>>t;
while(t--)
{
memset(vis,,sizeof(vis));
int n,m;
cin>>n>>m;
vis[n]=;
queue<point> q;
q.push((point){n,});
int flag1=;
while(!q.empty())
{
point now=q.front();
if(now.x==m)
{
flag1=now.y;
break;
}
point next;
for(int i=;i<=;i++)
{
next.x=now.x/;
next.x*=;
next.x+=i;
next.y=now.y+;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push(next);
} }
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
// cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
if(flag1>)
break;
q.pop();
}
printf("%d\n",flag1);
}
return ;
}

CD0J/POJ 851/3126 方老师与素数/Prime Path BFS的更多相关文章

  1. cdoj 851 方老师与素数 bfs

    方老师与素数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  2. poj 3126 Prime Path bfs

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

  3. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  4. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

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

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

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  8. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  9. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

随机推荐

  1. 十二、springcloud之展示追踪数据 Sleuth+zipkin

    一.Zipkin简介 Zipkin是Twitter的一个开源项目,它基于Google Dapper实现.我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的REST API接口来辅助我们 ...

  2. urllib2使用初探

    在入门urllib2之前,我想应该先调研一下urllib与urllib2的区别[1].首先我们要明白的是,这两个模块不可以相互替代.两者都是接受URL请求的模块,但是提供了不同的功能,两个显著的区别是 ...

  3. Java---容器基础总结

    Java提供了大量持有对象的方式: (1) 数组将数字与对象联系起来. 它保存类型明确的对象,查询对象时,不需要对结果做类型转换.它可以是多维的, 可以保存基本类型的数据. 但是,数组一旦生成,其容量 ...

  4. 关于int *a; int &a;a; int &a; *a; int * &a

    int i; int*a =&i;//这里a是一个指针,它指向变量i int&b = i;//这里b是一个引用,它是变量i的引用,引用是什么?它的本质是什么?下面会具体讲述 int*& ...

  5. 虚拟机的安装以及Linux的学习

    安装虚拟机 对虚拟机的认识 其实初中的时候我就听说过虚拟机这个名词,当时的我还小,也不知道虚拟机是个什么东西,那时我傻傻的认为虚拟机只不过是电脑中的一个虚拟的计算机,没有什么实在的作用.后来随着大学课 ...

  6. servlet 学习笔记(三)

    同一用户的不同页面共享数据有以下四种方法: 1.sendRedirect()跳转 2.session技术 3.隐藏表单提交(form) 4. cookie技术(小甜饼) --------------- ...

  7. thinkphp中导入和使用阿里云OSSsdk

    照做绝对行,在ThinkPHP中,第三方库都放在ThinkPHP/Library/Vendor/路径下. 1.下载OSS PHP SDK:https://help.aliyun.com/documen ...

  8. 全局设置axios发送cookie(axios 默认不发送cookie)

    import axios from 'axios' axios.defaults.withCredentials=true; 如图:

  9. Hadoop错误1(Text类型与String类型)

    在此类的博客中,博主主要记录的是在Hadoop实践过程中遇到的一些错误,先上一个代码 protected void map(Object key,Text value, Context context ...

  10. R语言实战(十)处理缺失数据的高级方法

    本文对应<R语言实战>第15章:处理缺失数据的高级方法 本文仅在书的基础上进行简单阐述,更加详细的缺失数据问题研究将会单独写一篇文章. 处理缺失值的一般步骤: 识别缺失数据: 检查导致数据 ...