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 1 Sample Output 1
3
1033 8179
1373 8017
1033 1033
6
7
0

大致意思就是由前面的那个素数变到后面的那个素数,每次只能变一位数,变化后的数也应该是一个素数(无论是不是所求的数),求变化次数。

直接BFS即可。

 //Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, s, res, ans, len, T, k;
int x, y;
int pr[maxn]; int P(int n) {
for(int i=; i*i<=n; i++) {
if( n%i== ) return ;
}
return ;
}
//将k位化0
int change(int n, int k) {
char s[] = {};
sprintf(s, "%d", n);
s[k] = '';
sscanf(s, "%d", &n);
return n;
} int solve(int s, int e) {
queue<int> q;
int dis[maxn] = {};
q.push(s);
dis[s] = ;
while( q.size() ) {
s = q.front();
q.pop();
if( s == e ) return dis[s]-;
int t = ;
for(int i=; i<; i++) {
int k = change(s, i);
for(int j=; j<; j++) {
int a = k+j*t;
if( pr[a]== && dis[a]== ) {
q.push(a);
dis[a] = dis[s]+;
}
}
t /= ;
}
}
return -;
} void input() {
for(int i=; i<maxn; i++) pr[i] = P(i);
cin >> T;
while( T -- ) {
cin >> x >> y;
ans = solve(x, y);
if( ans==- ) puts("Impossible");
else cout << ans << endl;
}
} int main(){
input();
return ;
}

Kattis之旅——Prime Path的更多相关文章

  1. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  2. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  3. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

  6. Prime Path(poj 3126)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  7. Prime Path(素数筛选+bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9519   Accepted: 5458 Description The m ...

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

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

  9. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

随机推荐

  1. 【开发者笔记】python中的类方法(@classmethod)和静态方法(@staticmethod)

    在java.c#等高级语言中我们用static来定义静态方法和静态变量,那么在python中如何定义静态方法和静态变量呢. python提供了@classmethod和@staticmethod来定义 ...

  2. Python开发【项目】:选课系统-改良版

    程序名称: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...

  3. log4j.properties配置详解与实例-全部测试通过[转]

    最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛没有一点格式,看着累.这里把网上收集到的整理了一下,并且全部都在机器上测试成功了.这么好的文档估计没有了吧? # ...

  4. win7 x64安装TensorFlow

    在windows下安装的TensorFlow做学习研究之用,如果要进行技术,请看相关博文:CentOS7安装TensorFlow 1.安装Pytho3.5 首先到Anaconda网站去下载Window ...

  5. TP增删改

    增加数据 <?php namespace Home\Controller; use Think\Controller; class IndexController extends Control ...

  6. Unity shader学习之高光反射光照模型

    高光反射光照模型的公式如下: Cspecular = Clight * mspecular * max(0, dot(v, r))gloss 要计算高光反射需要知道4个参数:入射光线颜色Cspecul ...

  7. python读取大文件

    最近在学习python的过程中接触到了python对文件的读取.python读取文件一般情况是利用open()函数以及read()函数来完成: f = open(filename,'r') f.rea ...

  8. HDU 6300

    Problem Description Chiaki has 3n points p1,p2,…,p3n. It is guaranteed that no three points are coll ...

  9. c扩展开发

    为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势. 另外C扩 ...

  10. flask请求钩子、HTTP响应、响应报文、重定向、手动返回错误码、修改MIME类型、jsonify()方法

    请求钩子: 当我们需要对请求进行预处理和后处理时,就可以用Flask提供的回调函数(钩子),他们可用来注册在请求处理的不同阶段执行的处理函数.这些请求钩子使用装饰器实现,通过程序实例app调用,以 b ...