**链接 : ** Here!

**思路 : ** 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大...


/*************************************************************************
> File Name: E.cpp
> Author:
> Mail:
> Created Time: 2017年11月26日 星期日 10时51分05秒
************************************************************************/ #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std; #define MAX_N 100000
int isPrime[MAX_N] = {0}, primeList[MAX_N] = {0};
int T;
int vis[MAX_N];
struct info {
info() {}
info(int num, int step) : num(num), step(step) {}
int num, step;
};
info st, ed; void init_prime() {
for (int i = 2 ; i < MAX_N ; ++i) {
if (!isPrime[i]) {
primeList[++primeList[0]] = i;
}
for (int j = 1 ; j <= primeList[0] ; ++j) {
if (i * primeList[j] >= MAX_N) break;
isPrime[i * primeList[j]] = 1;
if (i % primeList[j] == 0) break;
}
}
} // 在num的第i个位置上替换为j
int transInfo(int i, int j, int num) {
int temp[4], k = 3, ret;
while (num) {
temp[k--] = num % 10;
num /= 10;
}
temp[i] = j;
return temp[0] * 1000 + temp[1] * 100 + temp[2] * 10 + temp[3];
} int check(info p) {
if (p.num < 1000 || p.num >= 10000) return 0;
if (vis[p.num]) return 0;
if (isPrime[p.num]) return 0;
return 1;
} int BFS() {
queue<info> que;
vis[st.num] = 1;
que.push(st);
while (!que.empty()) {
info now = que.front();
que.pop();
if (now.num == ed.num) {
return now.step;
}
for (int i = 0 ; i < 4 ; ++i) {
for (int j = 0 ; j < 10 ; ++j) {
// 最高位不能为0
if (i == 0 && j == 0) continue;
info temp(transInfo(i, j, now.num), now.step + 1);
if (!check(temp)) continue;
vis[temp.num] = 1;
que.push(temp);
}
}
}
return 0;
}
void solve() {
memset(vis, 0, sizeof(vis));
st.step = 0;
int ret = BFS();
printf("%d\n", ret);
} int main() {
// freopen("./in.in", "r", stdin);
init_prime();
scanf("%d", &T);
while (T--) {
scanf("%d%d", &st.num, &ed.num);
solve();
}
return 0;
}

POJ 3126 Prime Path (BFS + 素数筛)的更多相关文章

  1. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

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

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

  3. poj 3126 Prime Path bfs

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

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

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

  5. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

  6. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  7. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  8. POJ 3126 Prime Path BFS搜索

    题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...

  9. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

随机推荐

  1. 输入法InputConnection

    /**  * The InputConnection interface is the communication channel from an  * {@link InputMethod} bac ...

  2. 1.7-BGP⑥

    BGP中的路由控制/过滤: LAB1:Distribute-list调用ACL(较落后) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      Step1:通过ACL定义 ...

  3. Swift代理和传值

    第一个视图控制器: import UIKit // 遵循协议 class ViewController: UIViewController,SecondVCDelegate { override fu ...

  4. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  5. SVM最通俗的解读

    摘自:https://www.zhihu.com/question/21094489/answer/86273196 什么是SVM? 当然首先看一下wiki. Support Vector Machi ...

  6. 2017 Multi-University Training Contest - Team 2&&hdu 6047 Maximum Sequence

    Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.demo.pojo.IdCard

    转自:https://blog.csdn.net/zheng0518/article/details/11029733 TestStudent.testSchemaExporttestSchemaEx ...

  8. Hardwood Species(map)

    http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...

  9. HDU3085 Nightmare Ⅱ

    题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...

  10. MAC软连接

    在mac上不设置环境变量有的时候也可以直接就访问到了某些文件.这个是为什么呢?答案是用了软连接. 1 查看加载文件 可以使用cat命令查看paths文件 cat etc/paths /usr/loca ...