POJ 3126 Prime Path
给定两个四位素数a b,要求把a变换到b
变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
求从a到b最少需要的变换次数。无法变换则输出Impossible
输入:
第一行 是一个数T 表示下面有T个测试数据
下面T行 每行两个数a b
解题思路:每次变换一次,个十百千,四个位置上的数每个从0-9循环一边一共需要4 * 10 = 40次循环 剪枝之后就没多少了 所以直接暴力BFS能过
下面是代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define N 10000
#define MAXV 10000
using namespace std;
bool Prime[];//Prime[i] = false证明 i 是素数
void MakePrime()//对素数打表
{
int i, j;
for(i = ; i <= N; i++) {
for(j = ; j < i; j++) {
if(i % j == ) {
Prime[i] = false;
break;
}
}
if(j == i) Prime[i] = true;
}
}
void BFS(int s, int e)
{
bool maps[N]; //用来记录此数是不是已经变换过
int step[N], d[], temp, e_temp, i, j; //d代表个十百千四个位置,step[i]用来储存变换为 i 时一共变换了几次
queue<int>q;
memset(step, , sizeof(step));
memset(maps, false, sizeof(maps));
q.push(s);
maps[s] = true;
while(!q.empty()) {
s = q.front();
q.pop();
d[] = s % ;//个
d[] = s / % ;//十
d[] = s / % ;//百
d[] = s / ;//千
for(i = ; i < ; i++) { //四个位置
temp = d[i]; //原本在此位置的数字 先用temp保存 因为后面如果改变的是其他位置这个位置不变
for(j = ; j < ; j++) { //0-9十个数
if(temp != j) { //自己不需要再次判断
d[i] = j;
e_temp = d[] + d[] * + d[] * + d[] * ;
if(!maps[e_temp] && Prime[e_temp]) { //判断这个数有没有判断过,是不是质数
maps[e_temp] = true;
step[e_temp] = step[s] + ;
q.push(e_temp);
}
if(e_temp == e) { //查找到 输出转换次数
printf("%d\n", step[e_temp]);
return;
}
}
d[i] = temp;
}
}
if(s == e) {
printf("%d\n", step[s]);
return;
}
}
//所有的数都变换过 无法得到结果
printf("Impossible\n");
}
int main()
{
MakePrime();
int start, end, T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &start, &end);
BFS(start, end);
}
}
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 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- 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(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
随机推荐
- Python Locust对指定网站“一键压测”
[本文出自天外归云的博客园] 前篇 前篇:Python Locust性能测试框架实践 本篇 承上——归纳过程 在前篇的基础上,我们可以利用Locust性能测试框架编写python脚本对指定网站或者接口 ...
- python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作
django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...
- oracle 锁表问题
oracle执行表数据更新的时候,会遇到锁表问题,比方说,会遇到这样的问题,主要原因是这张表被其他人占用,修改数据没有提交.oracle为了避免脏数据的产生,在其安全机制下,锁住该表. 执行如下操作, ...
- PHP调用webservice遇到 Soap WSDL Error - "failed to load external entity
本人遇到的原因是服务器上没有安装php-soap centos安装方式: yum install php-soap 安装完成后重启 php-fpm 服务 service php-fpm restart ...
- java里面interface,implement和extends的作用和用法
今天阅读<设计模式示例>,看到一段代码涉及到了interface,implements和extends,其实在C++中经常用到.今天特百度,比较了一下: interface是一个接口,类似 ...
- 输入5至10之间的数字(用javaScript实现判断)
输入5至10之间的数字 ----用javaScript实现判断 代码如下: <!DOCTYPE html><html><body> <script>fu ...
- python http代理代码
googlecode :https://code.google.com/archive/p/python-proxy/source/default/source # -*- coding: cp125 ...
- 在windows上搭建react-native的android环境
参考文档: http://facebook.github.io/react-native/docs/getting-started.html http://reactnative.cn/docs/0. ...
- SIGPIPE了解
当未连接成功相机网络,在socket请求中 if (_session->prepareSession("192.168.1.1") != ICH_SUCCEED) // ...
- 【Java】变量类接口_学习笔记
变量.类和接口 1.变量的类型 实例变量(不以static修饰) 成员变量 类变量(以static修饰) 所有变量 形参(方法签名中定义的变量) 局部变量 方法局部变量(在方法内定义) ...