POJ 3126 Prime Path 广度优先搜索 难度:0
http://poj.org/problem?id=3126
搜索的时候注意
1:首位不能有0
2:可以暂时有没有出现在目标数中的数字
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1e4+5;
const int inf=0x7fffffff;
bool prim[maxn];
void judge(){
prim[2]=true;
for(int i=3;i<maxn;i+=2)prim[i]=true;
for(int i=3;i<maxn;i+=2){
if(prim[i]){
for(int j=3;j*i<maxn;j+=2){
prim[j*i]=false;
}
}
}
}
int dp[maxn],s,e;
queue<int> que;
const int base[4]={
10,100,1000,10000
};
int change(int sta,int ind,int num){
int sub=sta%(base[ind]);
if(ind>0)sub-=sub%base[ind-1];
sta-=sub;
sta+=num*base[ind]/10;
return sta;
}
void cal(){
for(int i=0;i<maxn;i++)dp[i]=inf;
while(!que.empty())que.pop();
dp[s]=0;
que.push(s);
while(!que.empty()){
int f=que.front();que.pop();
if(f==e)return ;
for(int i=0;i<4;i++){
for(int j=0;j<10;j++){
if(i==3&&j==0)continue;
int to=change(f,i,j);
if(!prim[to]||dp[to]!=inf)continue;
dp[to]=dp[f]+1;
que.push(to);
}
}
}
}
int main(){
int T;
scanf("%d",&T);
judge();
while(T--){
scanf("%d%d",&s,&e);
cal();
printf("%d\n",dp[e]);
}
}
POJ 3126 Prime Path 广度优先搜索 难度:0的更多相关文章
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 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)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
随机推荐
- 【mlflow】打包:npm run build + python setup.py sdist
mlflow是一个开源机器学习平台 最近需要使用一个它的最新版本,但是这个最新版本没有git包,无法通过pip install安装,需要打包安装. 打包完之后在项目的dist文件夹中有打包后的压缩包, ...
- JAVA 对象内存结构
JAVA对象内存结构 HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header).实例数据(Instance Data)和对齐填充(Padding). 对象头 markWo ...
- python高级之Flask框架
目录: Flask基本使用 Flask配置文件 Flask路由系统 Flask模版 Flask请求与响应 Flask之Session Flask之蓝图 Flask之message 中间件 Flask插 ...
- 跟我学Makefile(四)
使用函数:函数调用,很像变量的使用,也是以“$”来标识的,其语法如下: $(<function> <arguments>) 或是 ${<function> < ...
- WebDriver API 实例详解(一)
一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...
- 一个简单的3D范例,是在别人基础上面整理的。
一个简单的范例,是在别人基础上面整理的.原来的例子,框图太乱了,没有条理感. http://pan.baidu.com/s/1eQTyGCE
- redis error MISCONF Redis is configured to save RDB snapshots
在操作命令incr时发生错误: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able ...
- jdk eclipse SDK下载安装及配置教程
原文地址https://blog.csdn.net/dr_neo/article/details/49870587 最新鲜最详细的Android SDK下载安装及配置教程 最近Neo突发神经,想要将学 ...
- sprintf函数的用法
说明1:该函数包含在stdio.h的头文件中,使用时需要加入:#include <stdio.h> 说明2:sprintf与printf函数的区别:二者功能相似,但是sprintf函数打印 ...
- HTML&CSS&Javascript脑图
今天看了极客学院的CSS3部分,加上前几天看过的HTML5部分,现在对HTML和CSS的基础有了系统的认识,正好发现这张图,简直Perfect! 感谢脑图的制作人,虽然不知道是谁,但能把HTML.CS ...