题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数。

线性筛一遍以后bfs就好。我写的双向,其实没有必要。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int maxn = 1e5;
bool isNot[maxn]; void seive(int n = maxn-)
{
int m = sqrt(n+0.5);
for(int i = ; i <= m; i++){
if(!isNot[i])
for(int j = i*i; j <= n; j += i){
isNot[j] = true;
}
}
isNot[] = isNot[] = true;
} int d[maxn];
int vis[][maxn],clk;
#define PB push_back
#define PS push
const int wei[] = {,,,}; void doubleBfs(int s,int t)
{
if(s == t) { puts(""); return; }
queue<int> Q[];
queue<int> *q1 = Q, *q2 = Q+, *nxt = Q+;
int *v1 = *vis, *v2 = vis[];
q1->PS(s); q2->PS(t);
v1[s] = ++clk; v2[t] = clk;
d[s] = ; d[t] = ;
while(q1->size() && q2->size()){
if(q1->size()>q2->size()){
swap(q1,q2); swap(v1,v2);
}
while(q1->size()){
int u = q1->front(); q1->pop();
int dig[];
for(int i = ; i < ; i++){
dig[i] = u/wei[i]%;
}
for(int i = ; i < ; i++){
int t = u - dig[i]*wei[i];
for(int j = i == ; j < ; j++){
if(j != dig[i]){
int v = t+j*wei[i];
if(!isNot[v] && v1[v] != clk){
if(v2[v] == clk) {
printf("%d\n",d[v]+d[u]+);
return;
}
d[v] = d[u]+;
nxt->PS(v);
v1[v] = clk;
}
}
}
}
}
swap(q1,nxt);
}
puts("Impossible");
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
seive();
int T; scanf("%d",&T);
while(T--){
int a,b; scanf("%d%d",&a,&b);
doubleBfs(a,b);
}
return ;
}

POJ 3126 Prime Path(筛法,双向搜索)的更多相关文章

  1. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  2. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  3. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  4. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  6. poj 3126 Prime Path(搜索专题)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 Desc ...

  7. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  8. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  9. (简单) POJ 3126 Prime Path,BFS。

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  10. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

随机推荐

  1. Lotus Notes 学习笔记

    这是一个学习关于如何使用Lotus Notes的Agent功能来实现自动化办公的学习笔记. 一. 介绍 Lotus Notes/Domino 是一个世界领先的企业级通讯.协同工作及Internet/I ...

  2. 用vue写一个仿简书的轮播图

    原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...

  3. linux下oracle一些常用命令

    dbca 配置数据库netca 配置tnslsnrctl status tns状态lsnrctl stop TNS停止lsnrctl start TNS启动

  4. Docker 快速安装&搭建 Ngnix 环境,并配置反向代理

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  5. 洛谷P2911 [USACO08OCT]牛骨头Bovine Bones【水题】

    题目大意:输入S1,S2,S3,随机生成三个数x,y,z,求x+y+z出现次数最多的数(如果有多个答案输出最小的),其中1<=x<=S1,1<=y<=S2,1<=z< ...

  6. Asp.net Core 创建控制器时出错问题记录(运行所选代码生成器时出错)

    问题描述:在创建一个MVC控制器(带读写,使用EF) 解决方法:添加nuget包

  7. netstat命令怎么查看端口是否占用

    转自:http://www.ahlinux.com/start/cmd/527.html netstat命令是一个监控TCP IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接 ...

  8. sql新增字段注意事项

    新增字段的类型.长度(精度)是否合适 解决方法: 跟应用明确加字段和改字段的风险,确认新增字段类型正确.长度(精度)合适. 以及跟应用明确老数据是否要订正?如何订正?新增列是否非空?是否有默认值等等. ...

  9. 03.Spring IoC 容器 - 初始化

    基本概念 Spring IoC 容器的初始化过程在监听器 ContextLoaderListener 类中定义. 具体由该类的的 configureAndRefreshWebApplicationCo ...

  10. 014 Longest Common Prefix 查找字符串数组中最长的公共前缀字符串

    编写一个函数来查找字符串数组中最长的公共前缀字符串. 详见:https://leetcode.com/problems/longest-common-prefix/description/ 实现语言: ...