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 ...
随机推荐
- er3
<html xmlns:v="urn:schemas-microsoft-com:vml"xmlns:o="urn:schemas-microsoft-com:of ...
- linux问题
make:进入目录'/opt/FriendlyARM/tiny4412/05android/android-4.1.2'target Dex: framework trouble writing ou ...
- AWK命令学习
使用方法 awk 'pattern {action}' {filenames} 尽管操作可能会很复杂,但语法总是这样,其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找到 ...
- msql 实现sequence功能增强
create table sequence ( seq_name VARCHAR(50) NOT NULL COMMENT '序列名称', min_val ...
- 【前端】移动端Web开发学习笔记【2】 & flex布局
上一篇:移动端Web开发学习笔记[1] meta标签 width设置的是layout viewport 的宽度 initial-scale=1.0 自带 width=device-width 最佳实践 ...
- No module named 'urllib2'
import urllib2 response = urllib2.urlopen('http://www.baidu.com/') html = response.read() print html ...
- CSS选择器、标签,div的位置。
今天是休假,布置了一个作业,是利用CSS制作斯坦福大学官网.虽然是一个并不复杂的制作,却让我第一次体会到了想摔鼠标的感觉. 遇到了很多问题,却找不出自己到底是哪里出了问题,简简单单的一个logo.足足 ...
- XAF视频教程来啦,已出15课
第一到第七课在这里: http://www.cnblogs.com/foreachlife/p/xafvideo_1_6.html 视频地址:http://i.youku.com/i/UMTI5OTE ...
- GCC for Win32开发环境介绍
http://blog.csdn.net/VisionCat/article/details/711693 http://blog.csdn.net/VisionCat/article/categor ...
- MySQL AHI 实现解析
版权声明:本文由musazhang原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/904925001482373849 来源 ...