[POJ]P3126 Prime Path[BFS]
[POJ]P3126
Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35230 | Accepted: 18966 |
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
Source
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ; ; namespace iNx{ struct qq{ int num; int c; }; qq q[Maxn]; int primer[Maxn],pos[Maxn]; bool check[Maxn],exist[Maxn]; int cnt,best; void Euler(){ memset(check,,sizeof check); int i,j; check[]=false ; ;i<;i++){ if(check[i]) primer[++cnt]=i; ;j<=cnt&&(i*primer[j]<);j++){ check[i*primer[j]]=false ; ) break ; } } } int getn(int a,int i){ ) ; ) )%; ) )%; ; } void bfs(int a,int b){ ,tail=; q[].num=a; q[].c=; int i,j,h,k,m,t; while(tail>=head){ h=q[head].num; if(h==b){ printf("%d\n",q[head].c); break ; } exist[h]=true ; ;i<=;i++){ k=getn(h,i); ,m=;j<i;j++) m*=; ;j<=;j++){ &&j==) continue ; if(j==k) continue ; t=h+j*m-k*m; if(check[t]&&(!exist[t])){ q[++tail].num=t; q[tail].c=q[head].c+; } } } head++; } } int main(){ Euler(); int n,a,b,i; scanf("%d",&n); ;i<=n;i++){ scanf("%d%d",&a,&b); memset(exist,,sizeof exist); bfs(a,b); } ; } } int main(){ iNx::main(); ; }
现在要开始天天练习了,蒟蒻我太难了。
[POJ]P3126 Prime Path[BFS]的更多相关文章
- 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 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
随机推荐
- flask 重定向详解
from flask import Flask,request,redirect,url_for app = Flask(__name__) @app.route('/') def hello_wor ...
- Shell 变量详解教程之位置变量与预定义变量
Shell 变量分为3部分,分别是用户自定义变量.位置变量和预定义变量. 一. 自定义变量 那么,什么是变量呢?简单的说,就是让某一个特定字符串代表不固定的内容,用户定义的变量是最普通的Shell ...
- Excel透视表基础之字段布局与重命名、更新、数字格式设置、空值与错误值、
字段布局与重命名 经典布局切换 字段布局 默认布局:文本类型在行区域.数字类型在值区域. 最好用鼠标拖拽. 字段重命名 可以在字段设置中更改. 透视表更新 延迟更新 手动刷新 自动刷新 刷新注意事项 ...
- 关于eclipse设置JRebel
版本:eclipse ee Version: 2018-09 (4.9.0) jrebel:最新2019-2 1.在eclipse->help->eclipse Marketplace 2 ...
- 洛谷 U78696 图书馆馆长的考验 题解
题面 1. 图书馆馆长的考验(library) 红魔馆的拥有者蕾米莉亚的好友帕秋莉是红魔馆的大图书馆的馆长.擅长操纵五行,名言是“万物都有属性.所谓的属性,和弱点是一样的”. 一天,因为魔理沙看了神之 ...
- java_时间戳与Date_相互转化的实现代码
转载自: java_时间戳与Date_相互转化的实现代码
- Homebrew学习(一)之初认识
Homebrew Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,会 ...
- Mac中如何查看电脑的IP地址
方法一:使用ifconfig命令 方法二:在charles中查看 Charles 的顶部菜单的 “Help”->”Local IP Address”,即可在弹出的对话框中看到 IP 地址,如下图 ...
- python 3 :list
2 List List中支持混合类型 number_list=[1,2,3,4,5] str_list = ["a","b","c",&qu ...
- phpstudy mysql数据连接不上(#1130)解决办法
问题:无论输什么密码,都显示#1130,找半天在终于在百度知道找到了,其他帖子都是水贴,暂时不知道为什么要这么加,反正加了重置服务就好了,重新打开phpMyAdmin 输入默认密码root既可 解决办 ...