题目链接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
*/

最新文章

  1. 使用FP-Growth算法高效发现频繁项集【zz】
  2. Flume_初识
  3. Fucking &quot;pkg-config not found&quot;
  4. 微信45028错误,微信has no masssend quota hint错误
  5. MFC中cannot find the definition (implementation) of this function 解决方法
  6. .net截取指定长度汉字超出部分以指定的字符代替
  7. 快速开启Safari的私密浏览(快捷键创建)
  8. Android短彩信源码解析-短信发送流程(二)
  9. jQuery克隆DOM节点
  10. uWSGI 踩坑记
  11. logstash配置
  12. 贝叶斯优化(Bayesian Optimization)深入理解
  13. Ajax爬虫必用到的字典转换器
  14. Python3自动化运维之Fabric模版详解
  15. semantic segmentation 和instance segmentation
  16. CentOs 6.6里kdump启动失败的原因
  17. windows安装 centos
  18. hdu4549 M斐波那契数列 矩阵快速幂+快速幂
  19. QT编写的网页浏览器网页乱码解决方法
  20. 开源中文分词框架分词效果对比smartcn与IKanalyzer

热门文章

  1. WebGoat学习——跨站脚本攻击(Cross‐Site Scripting (XSS))
  2. 拜托,这才是“Uber”的正确读法
  3. asp.net mvc让我告诉你请求从哪里来
  4. 30个有关Python的小技巧
  5. OC使用inline替代宏
  6. 应用删除后 Launchpad 上仍有应用图标无法删除的解决方法
  7. 《Java数据结构与算法》笔记-CH4-4循环队列
  8. 麒麟OS剽窃
  9. Codeforces 219D. Choosing Capital for Treeland (树dp)
  10. C#委托及事件处理机制浅析