一、题目

http://poj.org/problem?id=3126

二、分析

该题主要是要让我们找到一个$4$位素数到另一个$4$位素数的最少的变换次数,且要求保证每一次变换都满足

1.下一个数必须是4位。

2.下一个数必须是素数。

知道满足这两个条件后,然后结合$BFS$即可求出解。

这里有个处理4位数变换的技巧就是通过一个式子完成。

$next = P\%T[i]+P/T[i+1]*T[i+1]+j*T[i]$

这里的$T$数组为${1, 10, 100, 1000, 10000}$。具体的证明可以自己多画几项就出来了。

三、AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath> using namespace std;
const int MAXN = 1e5;
bool isPrime[MAXN];
bool visit[MAXN];
const int T[] = {, , , , };
int Start, End;
struct Node
{
int value;
int step;
}; void getPrime()
{
memset(isPrime, , sizeof(isPrime)); isPrime[] = isPrime[] = ;
for(int i = ; i < MAXN; i++)
{
if(isPrime[i])
{
for(int j = i*; j < MAXN; j+=i)
{
isPrime[j] = ;
}
}
}
} int BFS()
{
memset(visit, , sizeof(visit));
Node t;
t.value = Start;
visit[t.value] = ;
t.step = ;
if(t.value == End)
return t.step;
queue<Node> Q;
Q.push(t); while( !Q.empty() )
{
Node p = Q.front();
Q.pop();
t.step = p.step+;
for(int i = ; i < ; i++)
{
int temp, j;
temp = p.value%T[i] + (p.value/T[i+])*T[i+];
if( i == )
j = ;
else
j = ;
for(; j < ; j++)
{
t.value = temp + j*T[i];
if(!visit[t.value] && isPrime[t.value])
{
visit[t.value] = ;
if(t.value == End)
return t.step;
Q.push(t);
}
}
}
}
return -;
} int main()
{
int N, Ans;
getPrime();
scanf("%d", &N);
while(N--)
{
scanf("%d %d", &Start, &End);
Ans = BFS();
printf("%d\n", Ans);
}
return ;
}

POJ_3126 Prime Path 【BFS+素数打表】的更多相关文章

  1. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

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

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

  3. POJ 3126 Prime Path (BFS + 素数筛)

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

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

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

  5. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  6. POJ 3126:Prime Path(素数+BFS)

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  7. CD0J/POJ 851/3126 方老师与素数/Prime Path BFS

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 Descri ...

  8. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

  9. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

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

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

随机推荐

  1. python中Dict与OrderedDict

    使用dict时,Key是无序的.在对dict做迭代时,我们无法确定Key的顺序. 如果要保持Key的顺序,可以用OrderedDict: from collections import Ordered ...

  2. 在Windows里定时执行一个Python文件

    一.系统环境 操作系统:Win7 64位 二.说明 1.建立一个dos批处理文件 例: @echo off C: cd C:\work\python python aaa.py exit 2.利用Wi ...

  3. Nginx 模块开发

    Nginx 模块概述 Nginx 模块有三种角色: 处理请求并产生输出的 Handler 模块 : 处理由  Handler  产生的输出的 Filter (滤波器)模块: 当出现多个后台 服务器时, ...

  4. if else的执行流程

    int main(void) { int a, b; char op; float ans; scanf_s("%d%c%d",&a,&op,1,&b); ...

  5. SpringMVC——拦截器

    Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口 preHandle():这个方法在业务处理 ...

  6. 在VS2008和VS2010中禁用Visual Assist X

    此方法对于VS2008和VS2010 都适用. 在VS2008或VS2010菜单栏中选择“VassistX”选项卡,找到“Enable/Disable Visual Assist X”选项, 点击即可 ...

  7. 第16章-使用Spring MVC创建REST API

    1 了解REST 1.1 REST的基础知识 REST与RPC几乎没有任何关系.RPC是面向服务的,并关注于行为和动作:而REST是面向资源的,强调描述应用程序的事物和名词. 为了理解REST是什么, ...

  8. 快速入手Web幻灯片制作

    在线幻灯片 使用markdown可以快速的写出优美的文档,接下来我介绍一些简单的语法,快速的用浏览器制作幻灯片. 最基本使用格式 <!DOCTYPE html> <html> ...

  9. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  10. linux内存布局------深入理解计算机系统