CD0J/POJ 851/3126 方老师与素数/Prime Path BFS
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9982 | Accepted: 5724 |
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
Then for each test case, one line with two numbers separated by a
blank. Both numbers are four-digit primes (without leading zeros).
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff; //无限大
const int MAXN = ;
bool flag[MAXN];
int primes[MAXN], pi;
struct point
{
int x;
int y;
};
void GetPrime_1()
{
int i, j;
pi = ;
memset(flag, false, sizeof(flag));
for (i = ; i < MAXN; i++)
if (!flag[i])
{
primes[i] = ;//素数标识为1
for (j = i; j < MAXN; j += i)
flag[j] = true;
}
}
int vis[maxn];
int main()
{
GetPrime_1();
int t;
cin>>t;
while(t--)
{
memset(vis,,sizeof(vis));
int n,m;
cin>>n>>m;
vis[n]=;
queue<point> q;
q.push((point){n,});
int flag1=;
while(!q.empty())
{
point now=q.front();
if(now.x==m)
{
flag1=now.y;
break;
}
point next;
for(int i=;i<=;i++)
{
next.x=now.x/;
next.x*=;
next.x+=i;
next.y=now.y+;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push(next);
} }
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
// cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
if(flag1>)
break;
q.pop();
}
printf("%d\n",flag1);
}
return ;
}
CD0J/POJ 851/3126 方老师与素数/Prime Path BFS的更多相关文章
- cdoj 851 方老师与素数 bfs
方老师与素数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- [POJ]P3126 Prime Path[BFS]
[POJ]P3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35230 Accepted: ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
随机推荐
- 使用脚本实现killproc的功能
在shell提示符号下输入type killproc,会发现killproc实在 /sbin/目录下,通过man killproc可以查看这个脚本(姑且这么称为脚本)的用法,现在,把这个脚本的实现过程 ...
- 【前端vue开发】vue子调父 $emit (把子组件的数据传给父组件)
ps:App.vue 父组件 Hello.vue 子组件 <!--App.vue :--> <template> <div id="app"> ...
- Python_oldboy_自动化运维之路(八)
本节内容: 列表生成式,迭代器,生成器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器,生成器 1.列表生成式 #[列表生成] #1.列 ...
- Python匿名函数详解
python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的. 在Python中,lambda ...
- 洛谷P1120 小木棍(升级版)
传送门啦 一道经典的搜索剪枝题,不废话,步入正题. 分析: 一.输入时手动过滤不合法的情况 二.很明显我们要枚举把哪些棍子拼接成原来的长棍,而原始长度(原来的长棍的长度)都相等,因此我们可以在 $ d ...
- 使用JS实现文字搬运工
使用JS实现文字搬运工 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html><head><meta http-equiv=&quo ...
- MySQL学习笔记:definer与sql security
在以下例子中,出现definer于sql security invoker,导致不解,遂学习一翻. # 创建存储过程 DELIMITER $$ CREATE DEFINER = Hider@local ...
- 阿里百川码力APP监控 来了!
阿里百川码力APP监控 来了!这个APP监控 和手淘一起成长历经千锤百炼 走过千BUG万坑如今百川起产品 为了让你的APP更好 用户更爽! 在移动互联网时代,一款应用是否成功,用户体验是一个关键 ...
- ZCTF2015 pwn试题分析
ZCTF的pwn赛题分析, PWN100 这道题与SCTF的pwn100玩法是一样的,区别在于这个要过前面的几个限制条件.不能触发exit(0).否则就不能实现溢出了. 依然是触发canary来lea ...
- 第一次ActiveX Fuzzing测试
接着上一篇的看雪Exploit me试题. 这道题给出了一个ActiveX的DLL,挖掘这个DLL中的漏洞. 由于从来没有接触过ActiveX的Fuzzing,所以找了一些文章来看.自己动手试验了一下 ...