POJ 3126 Prime Path (素数+BFS)
题意:给两个四位素数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)的更多相关文章
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- poj 3126 Prime Path 【bfs】
题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path【BFS】
<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- idea 插件的使用 进阶篇(个人收集使用中的)
idea 插件的使用 进阶篇(个人收集使用中的) 恭喜你,如果你已经看到这篇文章,证明在idear使用上已经初有小成!那么就要向着大神进发了! 下边就是大神之路! 插件的设置 在 IntelliJ I ...
- Pool多进程示例
利用Pool类多进程实现批量主机管理 #!/usr/bin/python # -*- coding: UTF-8 -*- # Author: standby # Time: 2017-03-02 # ...
- SpringMVC学习笔记_02
1.springmvc对多视图的支持 (1)导入xml格式视图支持的jar包 注意:springmvc本身就支持xml格式,所以不用导入其他支持的jar包了. (2)在springmvc.xml中 ...
- 解决idea中找不到程序包和找不到符号的问题
问题如图: 解决方法: 将三处编码都设置成UTF-8,亲测有效 本人也是拜读大佬博客后解决的: http://www.cnblogs.com/wzhanke/p/4747966.html
- 工控安全入门之Modbus(转载)
工控安全这个领域比较封闭,公开的资料很少.我在读<Hacking Exposed Industrial Control Systems>,一本16年的书,选了的部分章节进行翻译,以其抛砖引 ...
- luogu 1052 过河
神仙的博客,先copy了日后绝对删掉的,(因为我实在没耐心看懂啊..) 题解 step 1理解题意 在做这道题之前,一定要理解好题意,有一个需要特别注意注意的地方: 青蛙不是一定要跳到石头上[嗯... ...
- JavaScript之HTML5 data-* 自定义属性[HTML5标准 node.dataset.attributeName]
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,使用这样的结构可以进行数据存放. 使用data-*可以解决自定义属性混乱无管理的现状. ...
- Android UI组件之自定义控件实现IP地址控件
http://www.cnblogs.com/razerlack/p/4273282.html
- Freemarker导出带多个不重复图片的word
1.新建一个word,添加一张图片,调整好图片大小与位置.
- 一步一步详解ID3和C4.5的C++实现
1. 关于ID3和C4.5的原理介绍这里不赘述,网上到处都是,可以下载讲义c9641_c001.pdf或者参考李航的<统计学习方法>. 2. 数据与数据处理 本文采用下面的训练数据: 数据 ...