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. python3中文转码方法

    python3中的转码,必须是byte类型的,str类型的会返回未定义方法. 示例代码如下 doc = pq(start_html.content) print("orig text=&qu ...

  2. 深入理解HashMap(及hash函数的真正巧妙之处)

    原文地址:http://www.iteye.com/topic/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...

  3. [android]解析XML文件的方法有三种:PULL,DOM,SAM

    PULL 的工作原理: XML pull提供了开始元素和结束元素.当某个元素开始时,可以调用parser.nextText从XML文档中提取所有字符数据.当解析到一个文档结束时,自动生成EndDocu ...

  4. IntelliJ IDEA + Maven + Tomcat 本地开发、部署、调试。

    1.maven 下载 解压 配置下 远程仓库( 用阿里云的 比较快).本地仓库 (可以本地C盘建立个文件夹当仓库).环境变量(方便使用maven命令)就可以了. 2.tomcat 下载 解压 配置下 ...

  5. 一键开关VS的release模式优化

    因为我们公司的项目规模非常大了,如果日常调试使用debug模式的话,每次调试启动都要非常长的时间,因此大多数人使用release关优化的方式来进行日常开发.但是因为持续集成的存在,上传的代码要求是开启 ...

  6. Effective STL 笔记 -- Item 9: Choose carefully among erasing options

    假设有一个容器中存放着 int ,Container<int> c, 现在想从其中删除数值 1963,可以有如下方法: 1: c.erase(remove(c.begin(), c.end ...

  7. stellar

    13) Sundapeng.123 12) 有个问题问下,这里的私钥和公钥是随意生成的吗? 当前的配置启动的时候报错了 11) ssh root@39.108.127.234 Liansen2018 ...

  8. ***codeigniter操作xml(Simplexml第三方扩展)

    This Simplexml class provides an alternative implementation of the SimpleXML API that works under PH ...

  9. Visual Studio 2017 发布 附带下载地址

    链接: https://pan.baidu.com/s/1kFjGwyj5HwabvmJKiyLF_g 提取码: 关注公众号[GitHubCN]回复获取    winform框架源码-Devexpre ...

  10. win10字体大小设置

    有时图形化界面不能正常显示,需要改变字体大小来查看 更改字体大小: 更改字体: