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 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
随机推荐
- PostgreSQL数据库的安装与PostGIS的安装(转)
原文:http://lovewinner.iteye.com/blog/1490915 安装postgresql sudo apt-get install postgresql-9.1 postgre ...
- kubernetes实战(四):k8s持久化安装rabbitmq集群
1.下载文件 https://github.com/dotbalo/k8s/ 2.创建namespace kubectl create namespace public-service 如果不使用pu ...
- 【Maven学习】Nexus私服代理其他第三方的Maven仓库
一.背景 [Maven学习]Nexus OSS私服仓库的安装和配置 http://blog.csdn.net/ouyang_peng/article/details/78793038 [Maven学习 ...
- Java压缩多个文件并导出
controller层: /** * 打包压缩下载文件 */ @RequestMapping(value = "/downLoadZipFile") public void dow ...
- vmware克隆虚拟机
克隆步骤 右键需要克隆的虚拟机 > 管理,在克隆向导中选择完整克隆. 实验环境:win10_64bit + vmware 12 pro + CentOS6.9_64bit 克隆之后网络配置 克隆 ...
- oracle 查询按月份分组
如下表table1: 日期(exportDate) 数量(amount) -------------- ----------- 14- ...
- 牛客国庆集训派对Day5 Solution
A 璀璨光滑 留坑. B 电音之王 蒙特马利大数乘模运算 #include <bits/stdc++.h> using namespace std; typedef long ...
- Codeforces Round #275 (Div. 2) 题解
A 题: 说的是在(LR) 之间找出ab互质 bc 互质 ac 不互质的 3个数 数据量小直接暴力 #include <iostream> #include <cstdio> ...
- Python笔记 #12# Dictionary & Pandas: Object Creation
Document of Dictionaries 10 Minutes to pandas tutorialspoint import pandas as pd data = [['Alex',10] ...
- 20145221 《Java程序设计》实验报告三:敏捷开发与XP实践
20145221 <Java程序设计>实验报告三:敏捷开发与XP实践 实验要求 以结对编程的方式编写一个软件,Blog中要给出结对同学的Blog网址 记录TDD和重构的过程,测试代码不要少 ...