POJ_3126 Prime Path 【BFS+素数打表】
一、题目
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+素数打表】的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- POJ 3126:Prime Path(素数+BFS)
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- CD0J/POJ 851/3126 方老师与素数/Prime Path BFS
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9982 Accepted: 5724 Descri ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
随机推荐
- CSS 内容生成
原文地址:http://www.zhangxinxu.com/wordpress/?p=739 一.哗啦哗啦的简介 zxx://这里“哗啦哗啦”的作用是为了渲染一种氛围.content属性早在CSS2 ...
- SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册
1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...
- Linux查看内存,负载状态
Linux查看内存,负载状态 查看内存使用情况 www.ahlinux.com cat /proc/meminfo MemTotal: 16332644 kB MemFree: ...
- c# 多维数组、交错数组(转化为DataTable)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- hrabs的数据库session的修改
using System;using System.Data;using System.Collections;using System.Collections.Generic;using Syste ...
- 在IE11(Win10)中检查up6.2配置
1.按F12,打开调试模式 2.打开调试程序选项卡 说明:在调试程序选项卡中可看到IE加载的脚本信息是否正确.因为IE有缓存,导致脚本有时不是最新的. 3.打开脚本,up6.js ...
- 第01章-Spring之旅
一.简化Java开发 1. Spring的主要特性 依赖注入DI和面向切面编程AOP. 2. 关键策略 轻量级和最小侵入性编程:POJO 松耦合:DI和AOP 声明式编程:切面和惯例 减少样板式代码: ...
- (转)ASP.NET基础之HttpHandler学习
原文地址:http://www.cnblogs.com/wujy/archive/2013/08/18/3266009.html 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP. ...
- LSI Storcli 工具使用
查看RAID卡ID 命令功能 查看LSI SAS3108RAID卡的ID. 命令格式 storcli64 show 使用实例 # 查看LSI SAS3108RAID卡的ID. [root@localh ...
- visual studio installer 卸载时不能删除安装目录问题
在vs中可以制作安装程序,但是这个安装程序默认卸载的时候不会把安装目录卸载,如果想在卸载的时候删除这个目录,那就要费点周折了.此方法同时适应于程序自删除以及工作目录删除. 基本思路是在程序要退出的 ...