BFS POJ 3126 Prime Path
/*
题意:从一个数到另外一个数,每次改变一个数字,且每次是素数
BFS:先预处理1000到9999的素数,简单BFS一下。我没输出Impossible都AC,数据有点弱
*/
/************************************************
Author :Running_Time
Created Time :2015-8-2 15:46:57
File Name :POJ_3126.cpp
*************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
bool is_prime[MAXN];
bool vis[MAXN];
int x, y, res;
struct Digit {
int d[];
int step;
}; void solve(void) {
memset (is_prime, true, sizeof (is_prime));
for (int i=; i<=; ++i) {
for (int j=; j<i; ++j) {
if (i % j == ) {
is_prime[i] = false; break;
}
}
}
} int get_num(int *a) {
int ret = ;
for (int i=; i<=; ++i) {
ret = ret * + a[i];
}
return ret;
} void BFS(int u, int v) {
Digit tmp;
for (int i=; i>=; --i) {
tmp.d[i] = u % ; u /= ;
}
tmp.step = ;
queue<Digit> Q; Q.push (tmp); vis[u] = true;
int ans = -;
while (!Q.empty ()) {
Digit x = Q.front (); Q.pop ();
int m = get_num (x.d);
if (m == v) {
ans = x.step; break;
}
for (int i=; i<=; ++i) {
for (int j=; j<=; ++j) {
if (i == && j == ) continue;
if (x.d[i] != j) {
Digit y = x;
y.d[i] = j; m = get_num (y.d);
if (is_prime[m] && !vis[m]) {
vis[m] = true; y.step++; Q.push (y);
}
}
}
}
}
if (ans == -) puts ("Impossible");
else printf ("%d\n", ans);
} int main(void) { //POJ 3126 Prime Path
solve ();
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &x, &y);
memset (vis, false, sizeof (vis));
BFS (x, y);
} return ;
}
BFS POJ 3126 Prime Path的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- 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 ...
随机推荐
- 守卫者的挑战(codevs 1997)
题目描述 Description 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如 ...
- [USACO5.3]巨大的牛棚Big Barn
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- Java内存分配与参数传递
JAVA中方法的参数传递方式只有一种:值传递. JAVA内存分配: 1.栈:存放 基本类型的数据.对象的引用(类似于C语言中的指针) 2.堆:存放用new产生的数据 3.静态域:存放在对象中用stat ...
- MongoDB使用教程收集(语法教程)
https://www.tutorialspoint.com/mongodb/index.htm https://wizardforcel.gitbooks.io/w3school-mongodb/c ...
- JDBC的存储过程
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/stored-procedure.html: 正如一个Connection对象创建了Statement和 ...
- Servlet自动刷新页面
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/auto-refresh.html: 假设一个Web页面,显示了现场比赛得分或股票市场状况或货币兑 ...
- cocos2d-x 3.0 CREATE_FUNC解析
#define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new __TYPE__(); \ i ...
- 改动select默认样式,兼容IE9
前面有篇文章已经提供了怎样改动select标签的默认样式,可是仅仅能兼容到ie10,要兼容ie9仅仅能模拟一个类似的 html结构: <div class="select_diy&qu ...
- 让你完全理解base64是怎么回事
HTTP将BASE64-编码用于基本认证和摘要认证,在几种HTTP扩展中也使用了该编码. Base-64编码保证了二进制数据的安全 Base-64编码可以将任意一组字节转换为较长的常见文本字符序列,从 ...
- Ubuntu18.04系统中vi键盘输入字符不匹配
起因 今天重装了我的雷神笔记本的ubuntu18.04,不要问我为什么,我就是想复习下重装系统而已.好吧,我承认我改错文件启动不起来了. 于是我要重装jdk.maven and so on,但是当我用 ...