题目链接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)的更多相关文章

  1. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  2. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  3. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  4. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  5. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  6. poj 3126 Prime Path(搜索专题)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 Desc ...

  7. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  9. (简单) POJ 3126 Prime Path,BFS。

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

随机推荐

  1. Visual Basic相关图书推荐

    Visual Basic从入门到精通(第2版) 作      者 国家863中部软件孵化器 编 出 版 社 人民邮电出版社 出版时间 2015-03-01 版      次 2 页      数 61 ...

  2. Hive QL

    转自http://www.alidata.org/archives/581 Hive 的官方文档中对查询语言有了很详细的描述,请参考:http://wiki.apache.org/hadoop/Hiv ...

  3. ubuntu14.04修复启动项

    自从安装了ubuntu14.04系统后(win7+ubuntu双系统),一直使用grub来引导win7和ubuntu,很长一段时间都使用的很好.突然前两天win7进不去了,无奈之下就用pe修复了win ...

  4. [转]天龙八部的BillingServer

    从字面上看,Billing是计费的,应该处理玩家在线时间或者包月之类.但是天龙八部是免费游戏,不需要算时间来计费.从代码中看,BillingServer也比较简单,它有一个连接到Web服务器,并监听一 ...

  5. 给一已经排序数组A和x,求A中是否包含两个元素之和为x

    亲爱的大神老爷们,这是小渣第一次写blog,欢迎大家来喷(批评指正),算法渣因为面试中连这道题都不会写就自己琢磨了一下,也参考了网上大家的做法 解法一: 思路:从首尾向目的靠拢,因为已经排序,[假设存 ...

  6. 理解OAuth 2.0 -摘自网络

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版.                                      本文对OA ...

  7. initWithSpriteFrameName和createWithSpriteFrameName

    /** * Initializes a sprite with a sprite frame name. <br/> * A cc.SpriteFrame will be fetched ...

  8. 【转】 ASP.NET网站路径中~(波浪线)解释

    刚开始学习ASP.NET的朋友可能会不理解路径中的-符代表什么,例如ImageUrl=”~/Images/SampleImage.jpg” 现在我们看看-代表什么意思.-是ASP.NET 的Web 应 ...

  9. jquery让一个点击事件刷新页面就自己执行一次的方法

    $('name')这个元素之前已经绑定过事件啦,(on绑定)然后直接调用下即可: $('name').click();

  10. hdu4421-Bit Magic(2-SAT)

    题意 根据图中公式由A[]构造B[][],现在给你B,问你存不存在一个数组A使之成立. 题解:对于每一位进行2-sat求解. 比赛半个小时时间,没做出来…… 一直T. 因为本身对算法不确定,所以也不知 ...