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. mac xcode 常见配置

    1.报错:There are no schemes in workspace "..." 设置scheme共享,方法: 2.Build 文件夹是中间文件的保存地方,如何设置在工程目 ...

  2. [Java in NetBeans] Lesson 06. Custom classes

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...

  3. async await的用法

    const fs = require('fs'); const readFile = function (fileName) { return new Promise(function (resolv ...

  4. MSSqlServer 发布/订阅配置(主从同步)

    背景: 1.单个独立数据库的吞吐量是有瓶颈的,那么如何解决这个瓶颈? 2.服务器直接数据如何复制.并具备一致性.可扩展性? 资源: 官方资源:https://technet.microsoft.com ...

  5. 6.C# 释放非托管资源2

    C# 释放非托管资源 C#中资源分为托管资源和非托管资源. 托管资源由垃圾回收器控制如何释放,不需要程序员过多的考虑(当然也程序员也可以自己释放). 非托管资源需要自己编写代码来释放.那么编写好的释放 ...

  6. 从零开始一起学习SLAM | 相机成像模型

    上一篇文章<从零开始一起学习SLAM | 为啥需要李群与李代数?>以小白和师兄的对话展开,受到了很多读者的好评.本文继续采用对话的方式来学习一下相机成像模型,这个是SLAM中极其重要的内容 ...

  7. Sklearn的使用

    初步接触要求时,从上图选自己数据所适用的方法, 首先看数据的样本是否 >50,小于则需要收集更多的数据 然后看问题适合分类.回归.聚类.降维中的哪一大类 Sklearn解决问题的一般步骤: 1. ...

  8. 47.HTML---frame,iframe,frameset之间的关系与区别

    iframe 是在html页面内嵌入框架 框架内可以连接另一个页面, 如 <html> <head></head> <body> <iframe ...

  9. C# JArray与JObject 的使用

    STEP1.using Newtonsoft.Json.Linq; STEP2 如何获取json里的某个属性(节点)值,对其删改,新增 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  10. Day10 Python网络编程 Socket编程

    一.客户端/服务器架构 1.C/S架构,包括: 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务)[QQ,SSH,MySQL,FTP] 2.C/S架构与socket的关系: 我们学习soc ...