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 ...
随机推荐
- 欧拉 路径&&回路
不管 欧拉回路 还是 欧拉路径 无向图或者有向图(删除方向后)要联通 欧拉路径存在的判定条件 1 无向图 度数为奇数的点最多有两个 2 有向图 最多只能有两个点的入度不等于出度 ...
- Codeforces 645B Mischievous Mess Makers【逆序数】
题目链接: http://codeforces.com/problemset/problem/645/B 题意: 给定步数和排列,每步可以交换两个数,问最后逆序数最多是多少对? 分析: 看例子就能看出 ...
- Ubuntu 16.04出现chmod: 无效模式:"a"的问题解决
命令: chmod a+x file1 提示:注意文件的类型,如果用在文件夹上是不行的,但是文件确实可以的.
- RabbitMQ集群环境搭建教程收集(待实践)
先收集,后续再实践. http://www.linuxidc.com/Linux/2016-10/136492.htm http://www.cnblogs.com/lion.net/p/572547 ...
- 【.Net 学习系列】-- 反射的简单用法
新建两个项目:类库(Model)和控制台应用程序(ReflectTest). 在[Model]中添加一个类[User]: namespace Model { public class User { p ...
- Kafka单机Windows环境搭建
Kafka单机Windows环境搭建 1,安装jdk1.8:安装目录不能有中文空格: 2,下载zookeeper,https://mirrors.cnnic.cn/apache/zookeeper/z ...
- MTK Camera驱动移植
对于MTK Camera驱动移植一般分为四部分: 1.硬件IO口配置: 2.Camera驱动移植: 3.上电时序. 4.改动i2c控制器: 硬件电路: 1.GPIO配置 打开 mediatek\dct ...
- Maven具体解释之仓库------本地仓库、远程仓库
在Maven中,不论什么一个依赖.插件或者项目构建的输出.都能够称之为构件. Maven在某个统一的位置存储全部项目的共享的构件.这个统一的位置.我们就称之为仓库.(仓库就是存放依赖和插件的地方) 不 ...
- C#在Linux下获取文件夹信息(所在磁盘总大小,使用空间,已用空间,使用率)
1.第一种使用shell命令实现: private DiskInfo LinuxGetFolderDiskInfo(string path) { DiskInfo disk = new DiskInf ...
- 在 Web 开发中,img 标签用来呈现图片,而且一般来说,浏览器是会对这些图片进行缓存的。
在 Web 开发中,img 标签用来呈现图片,而且一般来说,浏览器是会对这些图片进行缓存的. 比如访问百度,我们可以发现,图片.脚本这种都是从缓存(内存缓存/磁盘缓存)中加载的,而不是再去访问一次百度 ...