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】
<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...
随机推荐
- Android Studio:layout-sw600dp文件夹中创建activity_main.xml
1.右键res文件夹,新建Android resource directory文件夹 2.在resource type中选择layout 3.将Directory name命名为layout-sw6 ...
- 51nod 1050【DP】
思路: 就是先正常的dp一下求一个最大连续子串,然后特殊情况就是sum-最小连续子串.. 比一比谁大谁小就好了 #include <stdio.h> #include <string ...
- bzoj3195: [Jxoi2012]奇怪的道路(状压dp)
Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期有n座城市,编号为1..n.m条道路连接在这些城市之间,每 ...
- MyEclipse中安装SVN插件的最有效的方法
(1)下载svn插件:http://subclipse.tigris.org/files/documents/906/49209/site-1.8.8.zip (2)解压svn包,找到其中的两个文件夹 ...
- 深入解读docker网络与kubernetes网络
前言:你是否学习使用k8s很久很久了可是对于网络这块仍旧似懂非懂呢? 您是否对网上一堆帖子有如下的抱怨: 打开多个博客,然后发现有区别么? 明显是直译过来的,越看越迷糊 “因为xxx,所以yyy”,. ...
- 二分图最大匹配初探 By cellur925
一.什么是二分图 首先它需要是一张无向图. 之后它需要同时满足两个条件:①它的N个点被分为两个集合,且这两个集合交集为空:②同一集合内的点之间没有边相连. 二.无向图是否为二分图的判定 引理:无向图是 ...
- C++伪函数
#include <iostream> void say_hello() { std::cout << "hello world !" << s ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: #include<stdio.h> #inc ...
- BestCoder Round #54 (div.2) 1003 Geometric Progression
题目传送门 题意:判断是否是等比数列 分析:高精度 + 条件:a[i] * a[i+2] == a[i+1] * a[i+1].特殊情况:0 0 0 0 0是Yes的,1 2 0 9 2是No的 代码 ...
- 泛型generic