题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数。

思路:先打表求出四位长度的所有素数,然后利用BFS求解。从a状态入队,然后从个位往千位的顺序枚举下一个素数,入队,直到状态为b为止。

#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std; bool is_prime[10000];
bool visited[10000];
string a, b;
void sieve() { // 埃式筛法
memset(is_prime, true, sizeof(is_prime));
is_prime[0] = is_prime[1] = false;
int n = 9999;
for (int i = 2; i <= n; ++i)
if (is_prime[i])
for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
return;
}
struct number {
string num;
int step;
number(string num, int step) : num(num), step(step) {}
};
void solve() {
int ans = 0;
memset(visited, false, sizeof(visited));
queue<number> que;
que.push(number(a, 0)); // 初始状态
while (!que.empty()) {
number p = que.front(); que.pop();
if (p.num == b) { // 到达目标。。
ans = p.step;
break;
}
for (int i = 3; i >= 0; --i) { // 从个位往千位枚举
int jbegin = i == 0 ? 1 : 0; // 千位的时候,从1开始枚举
string c = p.num;
for (int j = jbegin; j <= 9; ++j) {
c[i] = j + '0';
int next = atoi(c.c_str()); // 下一个数字
if (!visited[next] && is_prime[next] && c != p.num) {
visited[atoi(c.c_str())] = true;
que.push(number(c, p.step + 1));
}
}
}
}
cout << ans << endl;
}
int main()
{
sieve();
int t;
cin >> t;
while (t--) {
cin >> a >> b;
solve();
}
return 0;
}

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

  1. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

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

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

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

  3. POJ - 3126 - Prime Path(BFS)

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

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

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

  5. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  6. poj 3126 Prime Path 【bfs】

    题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...

  7. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  8. POJ 3126 Prime Path【BFS】

    <题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...

  9. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

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

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

随机推荐

  1. Unity3d跨平台原理

    知乎的一个提问:unity3d跨平台原理 一些资料: IL IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe ...

  2. IDAPython安装

    转载:All Right   (本人没有测试过) 关于IDAPython的安装教程网上的资料非常少,也不是很详细,我费了好长时间才装好,现在和大家分享一下. 注意事项 下面几点关系到安装是否成功 ID ...

  3. modbus-vcr介绍

    相关链接:modbus-vcr modbus-vcr是一个Ettercap的插件,被使用在缺少数据完整性的工业控制系统协议方面. 这个Ettercap插件执行一个MITM攻击在使用Modbus协议的系 ...

  4. Linux - 服务基础

    /etc/init.d/sendmail start # 启动服务 /etc/init.d/sendmail stop # 关闭服务 /etc/init.d/sendmail status # 查看服 ...

  5. json和jsonp的使用区别

    json和jsonp的使用区别 一.    跨域请求的概念 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象. 二.    json和jsonp JSON是一种基于文本的数据交换方 ...

  6. cdn.dns,cms

    CDN CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各 ...

  7. win10如何获得管理员权限_百度经验

    win10如何获得管理员权限_百度经验http://jingyan.baidu.com/article/0aa223755448db88cd0d6450.html 在右下方任务栏的“搜索web和win ...

  8. js遍历对象的方法

    1. for ... in 语句 for (let variable in object)  { ... } https://developer.mozilla.org/zh-CN/docs/Web/ ...

  9. Web下文件上传下载的路径问题

    工程结构

  10. JavaScript中Function的拓展

    Function 是什么东西,就是JavaScript中的顶级类,系统级别的类.我们平时写的函数方法例如下. function Animal() { } Animal就是Function的实例,但是在 ...