A - Prime Path(11.1.1)

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
bool num[10001],used[10001];
int s,e;
void prim()//判断素数
{
memset(num,true,sizeof(num));
for(int i=2;i*i<10001;i++)
{
if(num[i])
{
for(int j=i+i;j<10000;j=j+i)
num[j]=false;
}
}
memset(num,false,1000);
}
struct node
{
int x,step;
};
int d[4]={1,10,100,1000};//位数
int bfs(int x)
{
queue<node> que;
node tp,num;
int tx;
num.x=x,num.step=0;
used[x]=false,que.push(num);
while(!que.empty())
{
tp=que.front();
que.pop();
for(int i=0;i<4;i++)
for(int j=0;j<=9;j++)
if(i==3&&j==0)
continue;
else
{
if(j!=(tp.x/d[i]%10))
{
tx=((tp.x/d[i]/10)*10+j)*d[i]+tp.x%d[i];
if(used[tx])
{
used[tx]=false,num.x=tx,num.step=tp.step+1,que.push(num);
if(num.x==e)
return num.step;
}
}
} }
}
int main()
{
int t;
cin>>t;
prim();
while(t--)
{
cin>>s>>e;
memcpy(used,num,10000);
if(s==e)
cout<<"0"<<endl;
else cout<<bfs(s)<<endl;
}
return 0;
}

(广度搜索)A - Prime Path(11.1.1)的更多相关文章

  1. 【搜索】 Prime Path

    #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include& ...

  2. [kuangbin带你飞]专题一 简单搜索 - F - Prime Path

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  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. poj 3126 Prime Path(搜索专题)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 Desc ...

  5. POJ 3126:Prime Path

    Prime Path Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  6. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  7. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

  8. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  9. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

随机推荐

  1. python新建txt文件,并逐行写入数据

    #coding=utf-8 txtName = "codingWord.txt"f=file(txtName, "a+")for i in range(1,10 ...

  2. 微软BI 之SSIS 系列 - 对于平面文件中 NULL 值处理过程中容易极易混淆的几个细节

    开篇介绍 最近有人问我有关文件处理中空值处理的相关问题: OLE DB Destination 中的 Keep Nulls 如何控制 NULL 值的显示? 为什么选中了 Keep Nulls 但是数据 ...

  3. 学习下知然网友写的taskqueue

    博主在他的博客里对taskqueue的各种使用情况和使用方法都介绍的很清楚:http://www.cnblogs.com/zhiranok/archive/2013/01/14/task_queue. ...

  4. Fiddler插件开发 - 实现网站离线浏览功能

    有这么一种应用场景: 你是做前端或APP开发的,需要调用服务端提供的接口,接口只能在公司内网访问:在公司外就无法调试代码了. 想在公司外访问怎么办呢? 如果在公司的时候将所有接口的响应内容都保存起来, ...

  5. 菜鸟教程之工具使用(七)——从GIt上导出Maven项目

    今天继续我们的工具教程,公司用Git作为版本控制工具,所以最近一直在跟Git打交道.也是一边学习一边使用,于是想做一些入门教程,一来自己总结一下,二来还能帮助一些刚刚接触Git的朋友.一举两得,何乐而 ...

  6. Python中的format()函数

    普通格式化方法 (%s%d)生成格式化的字符串,其中s是一个格式化字符串,d是一个十进制数; 格式化字符串包含两部分:普通的字符和转换说明符(见下表), 将使用元组或映射中元素的字符串来替换转换说明符 ...

  7. linux每日命令(6):rm命令

    rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. rm是一个危险的命令,使用的 ...

  8. pandas遍历行数据

    假设我的DataFrame如图所示: 我可以这样遍历它: for index,row in list.iterrows(): id =row["id"] x=row["x ...

  9. maven 如何引入本地jar包

    比如我下载了 一.怎么添加jar到本地仓库呢?步骤:1.cmd命令进入该jar包所在路径2.执行命令:mvn install:install-file -Dfile=lucene-queryparse ...

  10. CentOS重新加载网卡报错 Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/23

    最新文章:Virson's Blog 重新加载网卡时出现的错误如下: [root@vdb1 dev]# service network restart Shutting down interface ...