POJ 3126 Prime Path (BFS)
【题目链接】click here~~
【题目大意】给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数
【解题思路】和poj 3278类似。bfs+queue,分别枚举个十百千的每一位就能够了,只是注意个位仅仅能为奇数,且千位从1開始
代码:
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif #include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime> #if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif // C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector> #if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif using namespace std; #define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b? a:b
#define Min(a,b) a>b? b:a
#define mem(a,b) memset(a,b,sizeof(a)) typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=15000;
const int inf=0x3f3f3f3f;
int n,m,T; bool vis[N]; int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int dir6[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};///六个方向 inline LL read()
{
int c=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
return c*f;
} bool isPrime(int t)///素数推断
{
for(int i=2; i<=sqrt(t); ++i)
if(t%i==0) return false;
return true;
} struct node
{
int val,step;
}; void bfs()
{
mem(vis,false);
node a,b;
a.val=n;
a.step=0;
queue<node >vall;
vall.push(a);
while(!vall.empty()){
a=vall.front();
vall.pop();
if(a.val==m){
printf("%d\n",a.step);
return ;
}
int ge=a.val%10;///枚举个位
int shi=a.val%100/10;///枚举十位 for(int i=1; i<=9; i+=2){///个位
int y=(a.val/10)*10+i;
if(y!=a.val&&isPrime(y)&&!vis[y]){
b.val=y;
b.step=a.step+1;
vis[y]=1;
vall.push(b);
}
} for(int i=0; i<=9; i++){///十位
int y=(a.val/100)*100+i*10+ge;
if(y!=a.val&&isPrime(y)&&!vis[y]){
b.val=y;
b.step=a.step+1;
vis[y]=1;
vall.push(b);
}
} for(int i=0; i<=9; i++){///百位
int y=(a.val/1000)*1000+i*100+ge+shi*10;
if(y!=a.val&&isPrime(y)&&!vis[y]){
b.val=y;
b.step=a.step+1;
vis[y]=1;
vall.push(b);
}
} for(int i=1; i<=9; i++){///千位
int y=a.val%1000+i*1000;
if(y!=a.val&&isPrime(y)&&!vis[y]){
b.val=y;
b.step=a.step+1;
vis[y]=1;
vall.push(b);
}
}
} puts("Impossible");
return ;
}
int main()
{
T=read();
while(T--)
{
n=read(),m=read();
if(n==m) puts("0");
else bfs();
}
return 0;
} /*
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
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)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
随机推荐
- Swift相关图书推荐
Swift与Cocoa框架开发 作 者 [澳] 曼宁(Jonathon Manning),巴特菲尔德-艾迪生(Paris Buttfield 出 版 社 人民邮电出版社 出版时间 2015- ...
- EventSource (node.js 与 OC)
node.js服务器代码: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, ...
- js 多物体运动
<!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...
- Spark RDD概念学习系列之RDD的缺点(二)
RDD的缺点? RDD是Spark最基本也是最根本的数据抽象,它具备像MapReduce等数据流模型的容错性,并且允许开发人员在大型集群上执行基于内存的计算. 为了有效地实现容错,(详细见ht ...
- Chef
Chef是一个渐渐流行的部署大.小集群的自动化管理平台.Chef可以用来管理一个传统的静态集群,也可以和EC2或者其他的云计算提供商一起使用.Chef用cookbook作为最基本的配置单元,可以被泛化 ...
- 通过Microsoft Azure服务设计网络架构的经验分享(转)
原文:http://www.infoq.com/cn/articles/azure-networking-tips 本文从产品设计和架构角度分享了 Microsoft Azure 网络服务方面的使用经 ...
- c++面试题总结(1)
1.int a=5,则 ++(a++)的值是() A.5 B. 6 C.7 D.逻辑错误 a++返回的是一个临时变量,这里是右值,不能再前面++了 2.下面 ...
- C#UDP同步实例
差不多有一个礼拜总算有点进步. 代码很简单,只是为了实现功能. 网络上的资源是很多,除了不能用的,就是抄来抄去,是在乏味浪费时间. 说一下代码背景:实现的功能是发送端发送消息,接收端接收后立即响应,发 ...
- VS2010在C#头文件添加文件注释的方法(转)
步骤: 1.VS2010 中找到(安装盘符以C盘为例)C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCa ...
- leveldb
[LevelDB] LevelDB is a fast key-value storage library that provides an ordered mapping from string k ...