#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; int n, m;
const int N = 1e4 + 100;
int vis[N];
struct node
{
int x, step;
};
queue<node> Q; bool judge_prime(int x) //判断素数
{
if(x == 0 || x == 1)
return false;
else if(x == 2 || x == 3)
return true;
else
{
for(int i = 2; i <= (int)sqrt(x); i++)
if(x % i == 0)
return false;
return true;
}
} void BFS()
{
int X, STEP, i;
while(!Q.empty())
{
node tmp;
tmp = Q.front();
Q.pop();
X = tmp.x;
STEP = tmp.step;
if(X == m)
{
printf("%d\n",STEP);
return ;
}
for(i = 1; i <= 9; i += 2) //个位
{
int s = X / 10 * 10 + i;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 0; i <= 9; i++) //十位
{
int s = X / 100 * 100 + i * 10 + X % 10;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 0; i <= 9; i++) //百位
{
int s = X / 1000 * 1000 + i * 100 + X % 100;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 1; i <= 9; i++) //千位
{
int s = i * 1000 + X % 1000;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
}
printf("Impossible\n");
return ;
} int main()
{
int t, i;
scanf("%d",&t);
while(t--)
{
while(!Q.empty()) Q.pop();
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
vis[n] = 1;
node tmp;
tmp.x = n;
tmp.step = 0;
Q.push(tmp);
BFS();
}
return 0;
}

【搜索】 Prime Path的更多相关文章

  1. kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126

    题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...

  2. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

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

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

  4. (广度搜索)A - Prime Path(11.1.1)

    A - Prime Path(11.1.1) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64 ...

  5. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  6. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

  7. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  8. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  9. HDU - 1973 - Prime Path (BFS)

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

  10. POJ3126 Prime Path —— BFS + 素数表

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. 算法之LOWB三人组之冒泡排序

    排序 冒泡排序(Bubble Sort)时间复杂度为O(n^2) 列表每两个相邻的数,如果前面比后面大,则交换这两个数 一趟排序完成后,则无序区减少一个数,有序区增加一个数. def bubble_s ...

  2. 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确

    安装好后,登录后台提示 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确 检查mysql是否启动netstat -lnpt是否有3306端口? 一 有A 检查/www/wdlinu ...

  3. mro具体解释

    你真的理解Python中MRO算法吗? [前言] MRO(Method Resolution Order):方法解析顺序.Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会 ...

  4. mysql windows安装资源

    压缩包资源 https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/ 配置流程 https://blog.csdn.net/hel ...

  5. Unity时钟定时器插件——Vision Timer源码分析之一

    因为项目中,UI的所有模块都没有MonBehaviour类(纯粹的C#类),只有像NGUI的基本组件的类是继承MonoBehaviour.因为没有继承MonoBehaviour,这也不能使用Updat ...

  6. mysql系列(2)之 DDL语句

    1.创建数据库test1:create database test1; 2.查询系统中都存在哪些数据库:show databases; 3.选择数据库:use test1; 4.查看数据库中所有的表: ...

  7. echarts中间有字饼图Demo1

    echarts链接:http://gallery.echartsjs.com/editor.html?c=xHy2vIPzLQ 代码: option = { backgroundColor: 'bla ...

  8. idea不识别yml配置文件,怎么办?

      问题描述: 如下图,新建的springboot项目,添加了自定义的配置文件后,2.yml无法像上方文件的一样,被识别成配置文件! 虽然可能不会影响项目(不确定),但问题不解决,根本没有心情开始下一 ...

  9. mysql 数据库名称,中间带有中划线问题

    插入数据时候,引用了数据库名,数据库名中有横线,会提示错误: You have an error in your SQL syntax; check the manual that correspon ...

  10. android抽屉效果

    所谓抽屉  是区别于侧滑菜单 他不会把内容区域挤掉  他只是覆盖在内容区域 下边一个布局文件  一个代码   可以说的就是布局文件就是 <android.support.v4.widget.Dr ...