Prime Path

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 14091 Accepted: 7959

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

Northwestern Europe 2006

没什么好说的,BFS搜索,就是在搜索的时候,千位不能为零QAQ

#include <map>
#include <list>
#include <climits>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL unsigned long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int Max = 10010;
struct node
{
int x;
int num;
}; int n,m;
bool prime[Max];
bool vis[Max];
int bfs()
{
memset(vis,false,sizeof(vis));
node a,b;
a.num=0;
a.x=n;
queue<node>Q;
vis[n]=true;
Q.push(a);
while(!Q.empty())
{
a=Q.front();
Q.pop();
if(a.x==m)
{
return a.num;
}
for(int i=0;i<10;i++)
{
b.x=a.x/10*10+i;
b.num=a.num+1;
if(!vis[b.x]&&!prime[b.x])
{
vis[b.x]=true;
Q.push(b);
}
}
for(int i=0;i<10;i++)
{
int s=a.x%10;
b.x=a.x/100*100+i*10+s;
b.num=a.num+1;
if(!vis[b.x]&&!prime[b.x])
{
vis[b.x]=true;
Q.push(b);
}
}
for(int i=0;i<10;i++)
{
int s=a.x%100;
b.x=a.x/1000*1000+i*100+s;
b.num=a.num+1;
if(!vis[b.x]&&!prime[b.x])
{
vis[b.x]=true;
Q.push(b);
}
}
for(int i=1;i<10;i++)
{
b.x=a.x%1000+i*1000;
b.num=a.num+1;
if(!vis[b.x]&&!prime[b.x])
{
vis[b.x]=true;
Q.push(b);
}
}
}
return 0;
}
int main()
{
memset(prime,false,sizeof(prime));
prime[1]=true;
prime[0]=true;
m=sqrt(Max)+1;
for(int i=2;i<m;i++)
{
if(!prime[i])
{
for(int j=i*i;j<=Max;j+=i)
{
prime[j]=true;
}
}
}
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
printf("%d\n",bfs());
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏的更多相关文章

  1. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  2. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  3. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  4. JavaScript中的一些细节 分类: C1_HTML/JS/JQUERY 2014-08-05 16:45 384人阅读 评论(0) 收藏

    1.设置id / class等属性 用 setAttribute 设置一些常规属性如 id ,className 的时候经常不起作用,只能用 object.id = value 这样来设置 news_ ...

  5. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  6. Task schedule 分类: 比赛 HDU 查找 2015-08-08 16:00 2人阅读 评论(0) 收藏

    Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. android开发之broadcast学习笔记 分类: android 学习笔记 2015-07-19 16:33 32人阅读 评论(0) 收藏

    android中的广播用的太多了,今天稍微总结一下. 按注册方式分为两种: 1.静态注册广播: 静态注册广播就是在androidManifest.xml文件中注册广播,假设我们要实现这样一个效果,在一 ...

  8. AndroidManifest.xml中的application中的name属性 分类: android 学习笔记 2015-07-17 16:51 116人阅读 评论(0) 收藏

    被这个不起眼的属性折磨了一天,终于解决了. 由于项目需要,要合并两个android应用,于是拷代码,拷布局文件,拷values,所有的都搞定之后程序还是频频崩溃,一直没有找到原因,学android时间 ...

  9. JavaScript概念之screen/client/offset/scroll/inner/avail的width/left 分类: JavaScript HTML+CSS 2015-05-27 16:42 635人阅读 评论(0) 收藏

    原文地址:http://caibaojian.com/js-name.html JS中获取各种宽度和距离,常常让我们混淆,各种浏览器的不兼容让我们很头疼,现在就在说说js中有哪些宽度和距离. 1.名词 ...

随机推荐

  1. java中HashSet详解(转)

    HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...

  2. python自动化运维之路~DAY1

    python自动化运维之路~DAY1 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件大小单位单位换算 我们一起看一下下面的图: 没错,都是数字,而且这些数字都是二进制的数字 ...

  3. 面向切面编程AOP:基于XML文件的配置

    除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的. 首先建立一个类: package com.sevenhu ...

  4. II7下配置SSAS通过HTTP 远程链接访问

    IIS7下配置SSAS通过HTTP远程连接 安装环境操作系统:Windows7.Windows Server2008IIS版本:7.5 IIS7下配置SSAS通过HTTP远程连接详细的步骤如下:1.首 ...

  5. JAVA面试题之实现字符串的倒序输出

    package shb.java.demo; public class MyTest { public static void main(String[] args) { String string ...

  6. 软件需求分析之NABCD模型

    软件的特点:支持交友 N:想找到志同道合的人结伴出游,即可增进友谊,也可以提高出行的安全性. A:在景点下设置模块,可以看到其他人的出行计划,并可以相互交流,共同出游. B:这款软件可以让你交到朋友, ...

  7. paper 43 :ENDNOTE下载及使用方法简介

    转载来源:http://blog.sciencenet.cn/blog-484734-367968.html 软件下载来源: EndNote v9.0 Final 正式版:http://www.ttd ...

  8. DDR(二)

    DDR与SDRAM的最大区别:内部L-Bank的规格不同. SDRAM中的L-Bank存储单元的容量与芯片位宽相同, DDRAM中的存储单元的容量是芯片位宽的一倍. 所以一次的地址访问,可以进行2-P ...

  9. PAT乙级 1003. 我要通过!(20)

    答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1. ...

  10. zw版_Halcon图像库delphi接口文件

    zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下.     参见:& ...