**链接 : ** Here!

**思路 : ** 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大...


/*************************************************************************
> File Name: E.cpp
> Author:
> Mail:
> Created Time: 2017年11月26日 星期日 10时51分05秒
************************************************************************/ #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std; #define MAX_N 100000
int isPrime[MAX_N] = {0}, primeList[MAX_N] = {0};
int T;
int vis[MAX_N];
struct info {
info() {}
info(int num, int step) : num(num), step(step) {}
int num, step;
};
info st, ed; void init_prime() {
for (int i = 2 ; i < MAX_N ; ++i) {
if (!isPrime[i]) {
primeList[++primeList[0]] = i;
}
for (int j = 1 ; j <= primeList[0] ; ++j) {
if (i * primeList[j] >= MAX_N) break;
isPrime[i * primeList[j]] = 1;
if (i % primeList[j] == 0) break;
}
}
} // 在num的第i个位置上替换为j
int transInfo(int i, int j, int num) {
int temp[4], k = 3, ret;
while (num) {
temp[k--] = num % 10;
num /= 10;
}
temp[i] = j;
return temp[0] * 1000 + temp[1] * 100 + temp[2] * 10 + temp[3];
} int check(info p) {
if (p.num < 1000 || p.num >= 10000) return 0;
if (vis[p.num]) return 0;
if (isPrime[p.num]) return 0;
return 1;
} int BFS() {
queue<info> que;
vis[st.num] = 1;
que.push(st);
while (!que.empty()) {
info now = que.front();
que.pop();
if (now.num == ed.num) {
return now.step;
}
for (int i = 0 ; i < 4 ; ++i) {
for (int j = 0 ; j < 10 ; ++j) {
// 最高位不能为0
if (i == 0 && j == 0) continue;
info temp(transInfo(i, j, now.num), now.step + 1);
if (!check(temp)) continue;
vis[temp.num] = 1;
que.push(temp);
}
}
}
return 0;
}
void solve() {
memset(vis, 0, sizeof(vis));
st.step = 0;
int ret = BFS();
printf("%d\n", ret);
} int main() {
// freopen("./in.in", "r", stdin);
init_prime();
scanf("%d", &T);
while (T--) {
scanf("%d%d", &st.num, &ed.num);
solve();
}
return 0;
}

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

  1. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

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

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

  3. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  4. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  5. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

  6. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  7. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  8. POJ 3126 Prime Path BFS搜索

    题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...

  9. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

随机推荐

  1. VC ON_CONTROL_RANGE多个控件响应一个方法

    步骤/方法 分三个步骤 在头文件里声明函数比如 afx_msg void onNum(UINT uID) 在.cpp文件里加入函数体 void CCalculatorDlg::OnNum(UINT u ...

  2. [RxJS 6] The Retry RxJs Error Handling Strategy

    When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...

  3. 【翻译自mos文章】在12c数据库中,哪种audit trail 受到支持?

    在12c数据库中,哪种audit trail 受到支持? 来源于:What Audit Trail Types Are Supported For A 12c Database? (文档 ID 198 ...

  4. poj 1068 Parencodings(模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

  5. C++对象模型——关于对象(第一章)

    第一章    关于对象 在C语言中,"数据"和"处理数据的操作(函数)"是分开声明的,也就是说,语言本身并没有支持"数据和函数"之间的关联性 ...

  6. USACO Section 2.1 Ordered Fractions

    /* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...

  7. JavaScript探秘:强大的原型和原型链

    // foo 变量是上例中的 for(var i in foo) { if (foo.hasOwnProperty(i)) { console.log(i); } } JavaScript 不包括传统 ...

  8. linux中两个缓冲区

    不同于Windows,Linux系统里存在两个剪切板:一个叫做选择缓冲区(X11 selection buffer),另一个才是剪切板(clipboard). 01)选择缓冲区(缓冲内容在其他位置可用 ...

  9. 杂项:UML

    ylbtech-杂项:UML Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言 ...

  10. CentOs7 修改rpm安装背景图

    http://bbs.chinaunix.net/thread-4166176-1-1.html