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 ...
随机推荐
- 积累PDU
PDU,即专业发展单元PDUs(Professional Development Units)用来量化PMP所参与的学习和专业服务活动.典型情况下,每个小时所进行的有计划的.系统的学习或专业活动可获得 ...
- 从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题及解决的
#从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题的 ## 从前面发表了两篇博文,[webstorm+nodejs+JetBrains ID ...
- Windows Azure 虚拟网络配置(Point to Site)
说明:本文以Azure国际版为例,中国版在网络位置会存在一定差异. 1. 场景 虚拟网络为我们提供了在Windows Azure云计算环境上构建网络定义的能力,通过虚拟网络,我们可以方便地将Windo ...
- QCon 2013 上海 -- 高并发可用
高并发可用应该是这次QCon的主要议题,目测超过一半的话题都在讨论这个主题或者和这个主题相关.看到Yun关于AWS re:Invent的总结,好像这个在AWS上也是很热的一个主题.就我个人而言,没 ...
- poj 1581 A Contesting Decision
题目大意:有四个题目,有某些队做题,写一个判断程序如:Stars 2 20 5 0 4 190 3 220Stars是队名,2是提交的次数,20是花费的时间,花费时间为0则说明题目提交错误,错误的忽略 ...
- ubuntu 彻底删除软件包
找到此软件名称,然后sudo apt-get purge ......(点点为为程序名称),purge参数为彻底删除文件,然后sudo apt-get autoremove,sudo apt-get ...
- rhel及相关linux系统版本知识
Rhel 此处Rhel非等同redhat哦,RedHat是红帽公司在1994年左右开发维护的linux桌面版本,在2004年左右红帽公司放弃redhat开始进军linux服务器版本开发,具体见下截图 ...
- Wicket Hello World Example
A simple hello world example in Wicket, show the basic structure of Wicket web application. Tools an ...
- 设置UIButton文字大小颜色不同
_loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(iconX, CGRectGetMaxY(passwordBGView.frame)+25, ...
- ELF学习--装载过程
当我们在linux bash下执行ELF程序时,Linux系统是怎样装载和执行的呢? 1.bash进程fork出子进程 2.在bash的子进程中调用execve系统调用来执行指定的ELF. 3.exe ...