题意:给两个四位素数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. CodeForces - 163B Lemmings

    B. Lemmings time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. numpy笔记—np.sum中keepdims作用

    A = np.random.randn(4,3) B = np.sum(A, axis = 1, keepdims = True) 我们使用(keepdims = True)来确保 A.shape 是 ...

  3. ICS Hack Tools

    参考链接:http://icstraining.org/en/security-tools/configurations ICS-Security-Tool: https://github.com/I ...

  4. Mac改键软件Karabiner使用教程

    Mac改键软件Karabiner使用教程 目前Mac上比较好用的改键软件是Karabiner,不过对于最新的Sierra系统,Karabiner失效了.这里介绍的实际上是Karabiner-Eleme ...

  5. VMware workstation 网络选择 NAT模式 访问外网

    多年不用本地做测试  尽然被 nat 模式給卡着了  :动手的还是所以要记录一下: 1.根据自己需求创建 虚拟机 之后: 配置[网络适配器] -- 选择 nat 模式 ( 选择网卡 )   虚拟机   ...

  6. dubbo集群服务下一台服务挂了对服务调用的影响

    一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...

  7. asp.net mvc 中[Authorize]在IE9以上版本关于FormsAuthentication.SetAuthCookie无效的问题 解决方案

    简单的解决方法是,在网站根目录,新增一个浏览器定义文件(browser definition file) 叫“App_Browsers”文件夹,然后里面放一个“IE10.browser”文件即可,网站 ...

  8. joomla安装

    最开始我以为是我电脑反映慢.傻傻的等了很久.因为我在sae上面初始化成功了.只是差两张表而已.等了很久很久.也试了好几次.反正就是卡在创建数据表那里.突然我想到在sae初始化数据库的时候有两种模式In ...

  9. linux笔记_day06

    1.用户:表示符,凭证 2.用户组:表示符 进程也是有属主和属组的 安全上下文(secure context): 用户:UID,/etc/pawwd 组:GID ,/etc/group 影子口令: 用 ...

  10. Faster rcnn代码理解(4)

    上一篇我们说完了AnchorTargetLayer层,然后我将Faster rcnn中的其他层看了,这里把ROIPoolingLayer层说一下: 我先说一下它的实现原理:RPN生成的roi区域大小是 ...