Prime Path(POJ 3126 BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15325 | Accepted: 8634 |
Description

— 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
Output
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)的更多相关文章
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
- Prime Path (POJ - 3126 )(BFS)
转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82697622 作者:Mercury_Lc 题目链接 题意:就是给你一个n, ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)
传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- Linux 的系统运行级别
运行级别 说明 0 系统关机状态 1 单用户工作状态,用于root对系统进行维护,此时不予许其他用户使用主机.(类似于windows 的安全模式) 2 ...
- HDU 3516 Tree Construction (四边形不等式)
题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...
- XML Schema <第三篇>
验证XML文档是否符合议定的XML结构有两种方法,分别是DTD模式与XML Schema.本文主要介绍XML Schema. 一.XML Schema的优点 XML Schema基于XML,没有专门的 ...
- PowerShell因为在此系统中禁止执行脚本解决方法
PowerShell因为在此系统中禁止执行脚本解决方法 在Powershell直接脚本时会出现: 无法加载文件 ******.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 " ...
- SQL Interview Question
面试的时候发现会问一些SQL的基本问题,在此总结一下. ProgramInterview/SQL 这个网站上的问题还比较全. 1. Join type INNER JOIN: Returns all ...
- Android Call requires API level 11 (current min is 8)的解决方案
[错误描述] 在用Eclipse开发过程中,为了兼容Android2.2和4.0以上版本,我在使用Notification类时做了2个版本的代码,代码根据系统版本不同执行相应模块,结果,等我输完代码, ...
- hdu 5641 King's Phone(暴力模拟题)
Problem Description In a military parade, the King sees lots of new things, including an Andriod Pho ...
- Direct3D 2D文本绘制
现在学习下Direct3D在窗口中绘制一些文本信息,ID3DXFont接口负责创建字体和绘制二维的文本.我们介绍下ID3DXFont的用法. 1.创建LPD3DXFONT接口 LPD3DXFONT g ...
- 关于java读取和写入properties配置文件的内容
一般通过使用流的方式进行读取 代码示例如下: package com.zznode.transmit.util; import java.io.FileInputStream; import java ...
- python实现二叉树和它的七种遍历
介绍: 树是数据结构中很重要的一种,基本的用途是用来提高查找效率,对于要反复查找的情况效果更佳,如二叉排序树.FP-树. 另外能够用来提高编码效率,如哈弗曼树. 代码: 用python实现树的构造和几 ...