题意:给两个四位素数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. Cannot send, channel has already failed:

    背景: 一个同事往这个队列发数据,另一个同事从这个队列取数据,进行解析. 这是昨天同事昨天消费者 消费activemq 队列,一开始有正常,运行了一段时间后,发现突然消费者变为零了.因为有监控.之后怎 ...

  2. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  3. 基于Redis主从复制读写分离架构的Session共享(Windows Server)

    搭建主从复制 1.安装软件 下载Redis-x64-3.2.100.zip:https://github.com/MicrosoftArchive/redis/releases 第一步:将Redis拷 ...

  4. Python数据分析学习目录

    python数据分析学习目录 Anaconda的安装和更新 矩阵NumPy pandas数据表 matplotlib-2D绘图库学习目录                      

  5. comfirm和prompt的区别

    comfirm和prompt的区别. <html> <title>测试页面</title> <head> </head> <body& ...

  6. oracle_数据库对象

  7. windows安装anaconda 报错failed to create anacoda menu ?

    windows安装anaconda 报错failed to create anacoda menu ? 装了无数次,每次都是 failed to create anacoda menu然后无选择忽略, ...

  8. mono修改配置

    当前mono安装目录为:/home/mono,安装成功后修改配置需进入这个路径: cd /home/mono 1.修改TcpBinaryFrameManager.cs文件 cd /home/mono/ ...

  9. V4L2学习记录【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637600.html V4L2学习记录 这个还没有分析完,先在这放着,防止电脑坏掉丢了,以后再完善 V4L ...

  10. 字符驱动之二操作方法(struct file_operations)【转】

    转自:http://blog.chinaunix.net/uid-26837113-id-3157515.html 从上一篇我们看到了字符驱动的三个重要结构,那我现在跟大家详细的说说 struct f ...