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. mapreduce小结

    (不断更新) MapReduce架构是一种分布式编程架构,它本质上是将任务划分,然后归并.它是以数据为中心的编程架构,相比与分布式计算和并行计算等,它更看重的是吞吐率.它处理的数据是PB级的数据,它并 ...

  2. 转:Python获取随机数(英文)

    Random - Generate pseudo-random numbers Source code: Lib/random.py This module implements pseudo-ran ...

  3. UML: 部署图

    说部署图之前,先看看某24小时便利店管理系统的网络拓扑结构图: 这个图描述了本系统的整体物理结构,从该图我们可以得到以下信息:1.该便利店集团有总部和多个门店,总部管理财务.仓库.采购等事宜.2.二级 ...

  4. maven的中传递依赖,maven的依赖管理(转)

    在maven的pom文件中 <dependencies> <dependency> <groupId>junit</groupId> <artif ...

  5. javaIO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  6. cluster集群

    http://zwfang09.blog.sohu.com/entry/ Cluster 集群 ,群集 --- 负载均衡 LB ,load-balance --- 高可用集群 HA ,High Ava ...

  7. MySQL的基本函数

    charset(str) //返回字串字符集 mysql> select charset('demacia'); +--------------------+ | charset('demaci ...

  8. js获取单选框radio的值

    遇到一个js获取radio值的问题,原来根据frm.type.value取到的值在ie下是空值 解决办法:type为每个radio的值 var chkObjs=document.getElements ...

  9. 转:MyEclipse8.6插件安装方法

    通常,我们可以用update来直接安装.但是myeclipse限制了中国区的下载和更新.所以我们只能用插件配置的方法来实现. MyEclipse8.6插件安装同Eclipse插件安装方式大致相同,如下 ...

  10. SQLServer查询速度慢的原因

    查询速度慢的原因很多,常见如下几种:  1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应.  3.没有创建计算列导致查询不优化.  4.内存 ...