Prime Path

 

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

Source

 
 //2017-02-23
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node
{
int num, step;
}; bool isPrime(int n)
{
for(int i = ; i*i <= n; i++)
if(n%i==)return false;
return true;
} int pow(int a, int n)
{
int ans = ;
for(int i = ; i < n; i++)
ans *= a;
return ans;
} int main()
{
int n, x, y;
bool book[];
cin>>n;
while(n--)
{
cin>>x>>y;
queue<node> q;
node tmp;
tmp.num = x;
tmp.step = ;
q.push(tmp);
int num, step;
bool ok = false;
memset(book, , sizeof(book));
book[x] = ;
while(!q.empty()){
num = q.front().num;
step = q.front().step;
q.pop();
if(num == y){
ok = true;
cout<<step<<endl;
break;
}
for(int i = ; i < ; i++){
for(int p = ; p < ; p++){
if(i== && p==)continue;
int nowNum = num-((num/pow(, i))%)*pow(, i)+p*pow(, i);
if(!book[nowNum] && isPrime(nowNum)){
tmp.num = nowNum;
tmp.step = step+;
q.push(tmp);
book[nowNum] = ;
}
}
}
if(ok)break;
}
} return ;
}

POJ3126(KB1-F BFS)的更多相关文章

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

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

  2. poj3126 筛素数+bfs

    //Accepted 212 KB 16 ms //筛素数+bfs #include <cstdio> #include <cstring> #include <iost ...

  3. POJ3126 Prime Path(BFS)

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

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

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

  5. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  6. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  7. 巡逻机器人(BFS)

    巡逻机器人问题(F - BFS,推荐) Description   A robot has to patrol around a rectangular area which is in a form ...

  8. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  9. HDU5012:Dice(bfs模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the ...

  10. HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. TDDL与Spring Boot集成Version报错——跟踪与解决

    先说背景:公司采用diamond+tddl,这套技术来做web管理.本人处于好奇率先体验了下spring-boot,于是就有了spring-boot+tddl的组合.但是jar包上线后,屡屡发现一条e ...

  2. PHP之旅8 URL与表单

    表单 表单的出现让用户和后台主机有了直接的交互,网站开始变的‘动态起来’,在HTML的各个标记符中,与PHP关系最为紧密的要属表单标记符了,常见的表单标记符有<form>.<inpu ...

  3. word转html 压缩图片网站

    word转html https://docs.google.com/document/d/1MS-os1NcEPSEe2OWRenGR_6CsEmEQUchoQoh-abmL1Y/edit 压缩图片 ...

  4. 给button添加UAC的小盾牌图标

    Sample Code: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private v ...

  5. GDAL VS2010 win7(64位)安装、使用说明(图文解析)

    一.电脑配置及安装版本 Win 7(64位机) Visual Studio 2010 GDAL 1.9.2(我也尝试了最新版GDAL1.11.0,应该同样可以用的,只是在重新配置时又选用了老一点的版本 ...

  6. (转)Mysql 索引原理及优化

    本文内容主要来源于互联网上主流文章,只是按照个人理解稍作整合,后面附有参考链接. 一.摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引 ...

  7. Win7 Eclipse调试Centos Hadoop2.2-Mapreduce(转)

    一. 自己搭建开发环境 今天自己搭建了一套Centos5.3 + Hadoop2.2 + Hbase0.96.1.1的开发环境,Win7 Eclipse调试MapReduce成功.可能是版本比较高的原 ...

  8. puppet的使用:puppet配置文件介绍

    配置文件的产生 Puppet安装完后,配置文件就产生了,名称为puppet.conf,一般在/etc/puppet路径下. master也可以通过命令: puppet master --genconf ...

  9. ZigBee 学习资源

    1.雪帕的主页 http://home.cnblogs.com/u/yqh2007/ 2.刘志鹏的主页 http://www.cnblogs.com/hustlzp/archive/2011/02/1 ...

  10. 问题记录 | PyLint not recognizing cv2 members

    问题记录 | PyLint not recognizing cv2 members VScode中安装了pylint,总是提示cv2的一些成员函数找不到, 如这样的问题: Module 'cv2' h ...