题意:一个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. Spark Executor 概述

    Spark Executor 工作原理: 1. 在CoarseGrainedExecutorBackend启动时向Driver注册Executor,其实质是注册ExecutorBackend实例,和E ...

  2. bzoj 3722: PA2014 Final Budowa

    3722: PA2014 Final Budowa Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 303  Solved: 108[Submit][St ...

  3. 如何看待 Kotlin 成为 Android 官方支持开发语言

    Google IO 2017宣布了 Kotlin 会成为 Android 官方开发语言.一时间朋友圈和Android圈被各种刷屏.当然我也顺势而为发布了一篇的文章<为什么我要改用Kotlin&g ...

  4. 让你的spring-boot应用日志随心所欲--spring boot日志深入分析

    1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配 ...

  5. AT2045 Salvage Robots

    传送门 这个题只要想到移动机器人和移动出口是等价的就好做了 考虑设\(f[i][j][k][t]\)为最远向左移动\(i\),向右移动\(j\),向上移动\(k\),向下移动\(t\),这个矩形内最多 ...

  6. 入侵检测系统 - ossec

    http://www.cnblogs.com/zlslch/p/8512757.html

  7. 11gR2 ASM RAC + ASM RAC dataguard配置

    1.环境说明 --primary端配置 Primary RAC Node1 Node2 Public IP 192.168.56.120 192.168.56.122 Private IP 10.0. ...

  8. 7、python数据类型之集合set

    数据类型之集合setset 不允许重复的无序集合,不能通过下标取值,因为无序1.创建   创建空集合   s ={} 默认类型为字典,所以不是空集合,空集合如下   s = set()   s = { ...

  9. 2017年江西理工大学C语言程序设计竞赛(高级组)

    问题 A: 求近似值 #include <stdio.h> #include <time.h> #include <stdlib.h> using namespac ...

  10. 2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction

    Problem Description Kelukin is a businessman. Every day, he travels around cities to do some busines ...