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. 配置Windows Server 2008/2012/2016允许2个用户同时远程桌面

    Windows Server 系列服务器默认情况下只能支持一个用户远程,如果第二个人远程上去之后会直接把前面一个登录用户踢掉.在日常工作中如果有多个人需要同时远程过去工作,会很不方面. 网上很多教程讲 ...

  2. ASP.NET CORE下用盛派微信SDK取微信openid

    用CORE做项目用到微信的相关东西,听说那个盛派微信SDK很火,自己弄了下,只是简单的用用,用户访问页面取微信openid

  3. [ci]jenkins-slave-ssh docker容器化-自动注入key

    jenkins server 再启动slave时候,动态的注入sshkey 只要slave有ssh+jdk即可.无需事先预置用户名密码给slave. 配置 inject ssh key 配置项目 执行 ...

  4. [ci]jenkins-slave-ssh docker容器化-用户名密码

    jenkins-slave-ssh docker容器化 架构 参考:https://www.youtube.com/watch?v=OxrBCt1JLuQ https://github.com/Dav ...

  5. linux每日命令(27):chmod命令

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...

  6. Android下查看共享库依赖项

    Android下查看共享库依赖项 [时间:2017-02] [状态:Open] [关键词:android,共享库依赖项,so,ndk,objdump,readelf] 起因 近期在处理Android下 ...

  7. idea android 开发

    plugins 勾上 插件即可

  8. 【WPF】图片按钮的单击与双击事件

    需求:ListBox中的Item是按钮图片,要求单击和双击时触发不同的事件. XAML中需要引入System.Windows.Interactivity.dll xmlns:i="clr-n ...

  9. 模板, 保存&发布

    单击“视图”菜单中的“幻灯片模板”按钮,会弹出“幻灯片母版”选项卡,在此选项卡中,可以修改当前PPT的模板, 例如网上下载的幻灯片上的LOGO都可在这里去除. 模板设计总结 1. 背景可以选择纯色,也 ...

  10. 【Mac brew】代理安装brew insall

    http_proxy=dev-proxy.**.**:8080 https_proxy=dev-proxy.**.**:8080  brew install npm