一、题目

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. 383. Ransom Note 在字典数组中查找笔记数组

    [抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...

  2. 内存cache使用的场景

    Q.业务场景内为什么要使用内存cache? A.为了利用内存cache的优点, 解决业务场景内的缺陷 Q.内存cache的优点和缺点 A.优点: 内存读写速度比磁盘块 缺点: 内存空间有限, 内存单价 ...

  3. js颜色拾取器

    几年前,很难找到一个合适的颜色选择器.正好看到很多不错的JavaScript颜色选择器插件,故而把这些编译汇总.在本文,Web设计师和开发人员 Kevin Liew 选取了11个相应插件,有些会比较复 ...

  4. PyV8在服务端运行自动崩溃问题

    近来想在服务端架设WSGI + PyV8去自动解析JavaScript代码,然后返回解析后的数据给客户端.但是发现,在nginx配置后,客户端一请求,服务端的python脚本自动崩溃. 见代码: de ...

  5. getchar() getch() getche() gets() puts() scanf()的用法及区别

    getchar() putchar(ch) scanf()   头文件stdio.h getch() getche()   头文件conio.h gets() puts()    头文件stdio.h ...

  6. Visual Studio 2010调试本地 IIS 站点

    点击vs的Debug-Attach to Process选中 w3wp.exe,然后点击Attach, vs便进入debug模式.

  7. ASP.NET MVC 3 and the @helper syntax within Razor

    Friday, May 13, 2011 ASP.NET MVC 3 supports a new view-engine option called “Razor” (in addition to ...

  8. C# 随机数 Radom 循环生成同一的数字

    错误:在一个循环结构中,利用下列代码生成随机数,发生生成的随机数是一样的! for (int i = 0; i < myArray.Length; i++) //给数组赋值 { Random m ...

  9. scala冒泡排序

    scala冒泡排序: object Maopao { def main(args: Array[String]) { val list = List(1, 23, 432, 10, 23, 42, 3 ...

  10. Windows下配置Visualsvn Server时需要注意的几点事项

    1配置用户组与用户 用户组的权限高于用户的权限, 如果一个用户只有只读权限,同时被加入了拥有写权限的用户组中,此用户可以执行写操作. 2在Pre-commit hook下增加 强制添加注释的钩子脚本 ...