POJ - 3126 - Prime Path(BFS)
Prime Path
题意:
给出两个四位素数 a , b。然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止。要求 c 必须是素数。求变换次数的最小值。(a,b,c都是四位数字,输入时没有前导零)
分析:
- 每次改变可以获得一个四位数c,然后如果c是素数并且之前没有出现过,那么我们把它放入队列即可。
int f[10001];
int v[10001];
void init()//素数筛
{
memset(f,0,sizeof f);
for(int i=2;i<=10000;i++)
{
if(f[i]==0)
{
for(int j = i+i;j<=10000;j+=i)
{
f[j] = 1;
}
}
}
}
int a,b;
int bfs()
{
memset(v,0,sizeof v);
queue<int> q;
q.push(a);
v[a] = 1;
int tmp;
while(!q.empty())
{
tmp = q.front();q.pop();
if(tmp==b)return v[tmp];
int now;
int t = v[tmp]+1;
for(int i=0;i<=9;i++)//循环改变的数字 0 ~ 9
{
now = tmp - tmp%10+i;//改变个位
if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
now = tmp/100*100+i*10+tmp%10;//改变十位
if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
now = tmp/1000*1000+i*100+tmp%100;//改变百位
if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
now = i*1000+tmp%1000;//改变千位
if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
}
}
}
int main()
{
init();
int T;cin>>T;
while(T--)
{
cin>>a>>b;
int ans = bfs();
cout<<ans-1<<endl;
}
return 0;
}
POJ - 3126 - Prime Path(BFS)的更多相关文章
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- 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 (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- (简单) 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)
题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...
- poj 3126 Prime Path 【bfs】
题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...
- POJ 3126 Prime Path【BFS】
<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...
随机推荐
- keepalived+nginx高可用实现
1.keepalived介绍 keepalived最初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了实现高可用的VRRP功能.keepalived除了能 ...
- Linux下备份MySQL数据库的Shell脚本
数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...
- Zookeeper(1、3、5节点)集群安装
1节点 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储 3节点 hadoop-2.6.0.tar.gz的集群搭建(3节点) hadoop-2.6.0-cdh5. ...
- 2-zakoo使用
source:http://kazoo.readthedocs.io/en/latest/basic_usage.html 1 基本使用 1.1 连接处理 要使用zakoo,需要创建一个KazooCl ...
- python之list操作
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 列表(list)是最常用的Python数据类型,它可以作为一个方 ...
- (020)[虚拟系统]Win7网络连接红叉(无解决)
该虚拟机在重装主系统前是可以连接网络的,主系统重新安装以后,导入新安装的VM以后,网络图标显示红叉. 查看设备管理,显示没有安装以太网驱动. 重新安装 Vmware Tools,未果.VMware官网 ...
- RHEL 6.5---SVN服务实现过程
主机名 IP地址 master 192.168.30.130 slave 192.168.30.131 安装 [root@master ~]# yum install -y subversion h ...
- CSS实现文字旋转/实现角标
主要用到属性transform:rotate(-30deg) example: .divedittable .project-tag div { width: 43px; line-height: 4 ...
- RCC 2014 Warmup (Div. 1)
A 暴力 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- RPC之远程过程调用
一. 简介 将一个函数运行在远程计算机上并且等待获取那里的结果,这个称作远程过程调用(Remote Procedure Call)或者 RPC. RPC是一个计算机通信协议. 1. 类比: 将计算机服 ...