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 ...
随机推荐
- Neural Networks and Deep Learning(week3)Planar data classification with one hidden layer(基于单隐藏层神经网络的平面数据分类)
Planar data classification with one hidden layer 你会学习到如何: 用单隐层实现一个二分类神经网络 使用一个非线性激励函数,如 tanh 计算交叉熵的损 ...
- indexOf与includes的比较
indexOf和includes都代表检测数组或字符串中是否包含某一个元素 其中indexOf返回的是数值类型,而includes返回的是布尔类型 var ary = [,,]; console.lo ...
- 微信公众号绑定服务器 Flask版
python 代码 from flask import Flask, request from flask_cors import CORS app = Flask(__name__) app.app ...
- OpenCV在字符提取中进行的预处理(转)
OCR简介熟悉OCR的人都了解,OCR大致分为两个部分: -文字提取text extractor -文字识别text recognition 其中,第一部分是属于图像处理部分,涉及到图像分割的知识,而 ...
- Nginx 内核优化
内核参数的优化示例: /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = // timewait的数量,默认是180000. net.ipv4.ip_loca ...
- luogu P4744 [Wind Festival]Iron Man
再次感谢题解区大佬的指点 规定\(pre[i]\)表示前缀\(i\)的前缀和,\(sum[i][j]\)表示区间\([i,j]\)之和 令\(f[i][j]\)表示前i个数选出j段的最大值,\(g[i ...
- CF115B Lawnmower(贪心)
CF115B Lawnmower \(solution:\) 很明显的一道贪心题,奇数行只能向左走,偶数行只能向右走,每一行的起点应该在上一行就已确定,而这一行的终点只和(这一行最后一棵草(相对于你走 ...
- Synchronized和lock的区别和用法
一.synchronized和lock的用法区别 (1)synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要 ...
- php 获取压缩包名
参考链接: https://segmentfault.com/q/1010000000721799 通过curl方式获取压缩包名: function getFile($url, $save_dir = ...
- mysql原理~undo管理
一 简介:undo管理 二 各版本说明 1 5.5 undo位置:默认ibdata1中,不支持独立表空间 缺点:大事务可能造成ibdata1暴涨,只能dump导出导入或者从新搭建 参数: ...