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

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 <algorithm>
#include <queue>
using namespace std;
bool prime[];
bool vis[];
int n[];
int a,b;
struct node
{
int num,t;
};
void init()
{
int i,j;
memset(prime,,sizeof(prime));
for(i=;i<;i++)
{
for(j=;j<i;j++)
{
if(i%j==)
break;
}
if(j==i)
{
prime[i]=; //1代表质数
//cout<<i<<endl;
}
}
for(i=;i<;i++)
n[i]=i;
n[]=,n[]=,n[]=,n[]=;
return;
}
int bfs()
{
int i,j;
queue<node> Q;
node tem,u;
int k;
tem.num=a,tem.t=;
memset(vis,,sizeof(vis));
Q.push(tem);
while(!Q.empty())
{
tem=Q.front();
Q.pop();
//cout<<tem.num<<endl;
/*if(tem.num==3733)
printf("adsfadsf\n");*/
if(tem.num==b)
return tem.t;
for(i=;i<=;i++)
{
if(tem.num%==n[i])
continue;
u.num=tem.num/;
u.num=u.num*+n[i];
//cout<<u.num<<" "<<prime[u.num]<<" "<<vis[u.num]<<endl;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=(tem.num/)%;
if(k==n[i])
continue;
k=tem.num%;
u.num=((tem.num/)*+n[i])*+k;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=(tem.num/)%;
if(k==n[i])
continue;
k=tem.num%;
u.num=((tem.num/)*+n[i])*+k;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=tem.num/;
if(k==n[i])
continue;
//cout<<k<<" "<<n[i]<<endl;
k=tem.num%;
u.num=n[i]*+k;
// cout<<u.num<<endl;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
//cout<<u.num<<endl;
Q.push(u);
}
}
//break;
}
return -;
}
int main()
{
int T,ans;
init();
freopen("in.txt","r",stdin);
//freopen("ou.txt","w",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
ans=bfs();
if(ans==-) printf("Impossible\n");
else printf("%d\n",ans);
}
}

Prime Path(POJ 3126 BFS)的更多相关文章

  1. Prime Path (poj 3126 bfs)

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

  2. Prime Path (POJ - 3126 )(BFS)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82697622     作者:Mercury_Lc 题目链接 题意:就是给你一个n, ...

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

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

  4. Prime Path(poj 3126)

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

  5. (广度搜索)A - Prime Path(11.1.1)

    A - Prime Path(11.1.1) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64 ...

  6. POJ 3126 Prime Path(筛法,双向搜索)

    题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...

  7. UVa 1599 Ideal Path (两次BFS)

    题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...

  8. 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)

    传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...

  9. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

随机推荐

  1. mysql 高效分页控件及c#调用实例

    第一.首先在mysql中创建一个存储过程 BEGIN /* @selectSql VARCHAR(5000), --sql语句 @orderWhere VARCHAR(200), --排序条件 @pa ...

  2. Qemu对x86静态内存布局的模拟

    快乐虾 http://blog.csdn.net/lights_joy/ lights@hb165.com 本文适用于 QEMU-0.10.5 VS2008 欢迎转载,但请保留作者信息 在PC机中,由 ...

  3. Visual studio 使用正则表达查找替换

    原文 http://www.cnblogs.com/shineqiujuan/archive/2012/07/04/2575535.html 正则表达式是查找和替换文本模式的一种简洁而灵活的表示法.  ...

  4. 页和区 sql server

    原文地址:http://msdn.microsoft.com/zh-cn/library/ms190969.aspx SQL Server 中数据存储的基本单位是页.为数据库中的数据文件(.mdf 或 ...

  5. 学习javascript基础知识系列第三节 - ()()用法

    总目录:通过一段代码学习javascript基础知识系列 注意: 为了便于执行和演示,建议使用chrome浏览器,按F12,然后按Esc(或手动选择)打开console,在console进行执行和演示 ...

  6. Ubuntu Eclipse的Tomcat小问题:不能输入server name,不能启动tomcat

    Ubuntu的Eclipse上安装Tomcat环境,这是让人烦啊,万幸还是终于解决了. Eclipse上Tomcat的搭建: 1.点击Eclipse上的菜单:Windows - Preference, ...

  7. 第02讲- Android开发环境

    第02讲Android开发环境 需要下载的软件: JDK(JavaDevelopment Kit) Eclipse AndroidSDK(SoftwareDevelopmentKit) ADT(And ...

  8. yii2学习的论坛

    http://www.yiifans.com/forum.php http://www.yiichina.com/ http://www.yiichina.com/

  9. UGUI实现摇杆(模仿太极熊猫)

    核心代码: using UnityEngine; using System.Collections; using UnityEngine.UI; public delegate void Joysti ...

  10. python高级编程之选择好名称:完2

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #分解代码 #小就是美,这也适用所有级别的代码,当一个函数,类或者一 ...