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. 可以ping通,浏览器打不开网页 - 解决办法

    网络故障表现为: 1.电脑显示网络连接正常,DNS配置和hosts配置均正常 2.cmd可以ping通网址,域名 3.所有浏览器无法打开网页,有道云笔记置灰,微信二维码刷新失败 解决办法: 管理员权限 ...

  2. 测试jsp

    一. get.jsp <%@ page contentType="text/html;charset=UTF-8"%> <%@ page import=" ...

  3. DragonBoard810使用记录

    1. 执行~/workdir/Source_Package$ getSource_and_build.sh后该脚本先下载android仓库.repo到~目录,然后将android源码check out ...

  4. Hdu4742-Pinball Game 3D(cdq分治+树状数组)

    Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...

  5. OpenWrt openssl library

    Please install the openssl library (with development headers) sudo apt-get install libssl; rite fail ...

  6. docker初步

    [Note,]由于docker的局限性,docker只能运行在64位的系统中 docker软件应用程序可以重复地运行在任何地方,因为它的容器包含了所有的环境依赖关系! docker有三种方式运行 作为 ...

  7. 12 个 Linux 进程管理命令介绍

    执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...

  8. Handsontable对单元格的操作

    1.自动填充单元格数据 fillHandle:true/false    //当值为true时,允许拖动单元格右下角,将其值自动填充到选中的单元格 2.合并单元格 mergeCells:[{row:起 ...

  9. jqGrid源代码分析(一)

    废话少说.先上grid.base.js 整体结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3B5MTk4ODEyMDE=/font/5a6L5L2 ...

  10. PHP你可能也会掉入的坑

    今天被人问: $var = 'test'; if (isset($var['somekey'])) { echo 'reach here!!!'; } 会不会输出'reach here!!!'? -- ...