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】
<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...
随机推荐
- 洛谷 - P1739 - 表达式括号匹配 - 模拟 - 栈
https://www.luogu.org/problemnew/show/P1739 虽然应该是用栈的……但是直接模拟就可以了. #include<bits/stdc++.h> usin ...
- C++笔试题(十)
这些题目相比其他公司的试题,较为基础,全部为C语言,没有涉及C++,但如果不细心,是很难得到较高分数的.另外大家转贴不要去掉我的个人信息啊.互相宣传下网站嘛.1. 找错 void test1() { ...
- [转载]OI省选算法汇总
简单列了一点 1.1 基本数据结构 数组 链表,双向链表 队列,单调队列,双端队列 栈,单调栈 1.2 中级数据结构 堆 并查集与带权并查集 hash 表 自然溢出 双hash 1.3 高级数据结构 ...
- adb相关
--------------------------------------------- adb logcat |find "nafio" >c:/logcat.txt 另 ...
- Codeforces732E Sockets
首先检测有木有和Computer匹配的Socket,如果有则将其匹配. 然后将所有没有匹配的Socket连上Adapter,再去检测有木有Computer与Socket匹配. 重复这个操作31次,所有 ...
- Luogu P2326 AKN's PPAP【按位贪心】
题目描述 “I have a pen,I have an apple.Eh,Apple-Pen!. I have a pen,I have pineapple.En,Pineapple-Pen! Ap ...
- hdu 3484 Interviewe RMQ+二分
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; + ...
- memcache的分布式配置
public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { St ...
- ubuntu中mysql安装失败
在ubuntu中mysql安装失败后,卸载重新安装还是安装失败,之后找了资料说是卸载的不干净,然后进行下面操作,重新安装成功. 解决办法如下: sudo rm /var/lib/mysql/ -Rsu ...
- Andriod 简介
Andriod系统采用分层架构,分为4层: 应用程序层(Applications) 包含所有安装在手机上的应用程序(包括系统自带的程序) 应用程序框架层(Application Framework) ...