题意:给定两个四位素数作为终点和起点,每次可以改变起点数的某一位,且改变后的数仍然是素数,问是否可能变换成终点数字?

思路:bfs搜索,每次改变四位数中的某一位。素数打表方便判断新生成的数是否是素数。

AC代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int maxn = 1e5 + 5;
int vis[maxn], d[maxn];

void deal(int n){
	int m = sqrt(n + 0.5);
	memset(vis, 0, sizeof(vis));
	for(int i = 2; i <= m; ++i) if(!vis[i])
		for(int j = i*i; j <= n; j += i) vis[j] = 1;
}

int x, y;

void cut(int n, int *a) {
	int ind = 3;
	while(n > 0) {
		a[ind--] = n % 10;
		n /= 10;
	}
}

int rev(int *a) {
	int n = 0;
	for(int i = 0; i < 4; ++i)
		n = n * 10 + a[i];
	return n;
}

int bfs() {
	memset(d, -1, sizeof(d));
	d[x] = 0;
	queue<int>q;
	q.push(x);
	while(!q.empty()) {
		int n = q.front();
		q.pop();
		if(n == y) return d[y];
		int a[4];
		cut(n, a);
		for(int i = 0; i < 4; ++i){
			int b[4];
			memcpy(b, a, sizeof(b));
			for(int j = 0; j <= 9; ++j) {

				b[i] = j;
				int num = rev(b);
				if(num > 10000 || num < 1000 || vis[num] || d[num] != -1) continue;
				d[num] = d[n] + 1;
				q.push(num);
			}
		}
	}
	return -1;
}

int main(){
	deal(maxn);
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &x, &y);
		int ans = bfs();
		if(ans == -1) printf("Impossible\n");
		else printf("%d\n", ans);
	}
	return 0;
}

如有不当之处欢迎指出!

POJ - 3126 bfs + 素数筛法 [kuangbin带你飞]专题一的更多相关文章

  1. HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二

    题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...

  2. HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二

    类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...

  3. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  4. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. POJ - 2251 bfs [kuangbin带你飞]专题一

    立体bfs,共有六个方向: const int dx[] = {0,0,1,-1,0,0}; const int dy[] = {1,-1,0,0,0,0}; const int dz[] = {0, ...

  7. POJ - 3414 bfs [kuangbin带你飞]专题一

    状态搜索,每种状态下面共有六种选择,将搜索到的状态保存即可. d[i][j]表示状态A杯中水i升,B杯中水j升,总状态数量不会超过A杯的容量 * B杯的容量. AC代码 #include<cst ...

  8. POJ - 3984 bfs [kuangbin带你飞]专题一

    bfs搜索过程中将路径保存下即可. AC代码 #include<cstdio> #include<cstring> #include<algorithm> #inc ...

  9. 【算法系列学习】[kuangbin带你飞]专题二 搜索进阶 D - Escape (BFS)

    Escape 参考:http://blog.csdn.net/libin56842/article/details/41909459 [题意]: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消 ...

随机推荐

  1. ECLIPS-S测井系统下的仪器挂接 [CV模块]

    常见简写 简写 全拼 含义 CV Calibration and Verification 刻度和校验 CP Primary Calibration 主刻度 VP Primary Verify 主校验 ...

  2. test for python thread

    #!/usr/bin/python # -*- coding: UTF-8 -*- import thread import time # 为线程定义一个函数 def print_time(threa ...

  3. ClearCase config_spec

    1.使用分支前要在vob创建branch  type,Config_Spec不能自动创建branch type: 2.如果可能,最好在以前确定的label上进行新的工作,避免维护复杂的config_s ...

  4. Css3:transform变形

    transform 语法: transform      向元素应用 2D 或 3D 转换. transform : none | <<span class="title&quo ...

  5. 04_Javascript初步第二天(下)

    错误对象 try{ aa();//这是一个未被定义的方法 }catch(e){ alert(e.name+":"+e.message);//输出:ReferenceError:aa ...

  6. iometer测试工具

    简介 Iometer 为计算机I/O子系统所作的工作就如同测力计为引擎所作的工作一样:它测定在可控制的负荷下系统的性能.Iometer 以前被称为"伽利略". Iometer 既是 ...

  7. JVM类加载机制---类加载的过程

    一.类加载的时机 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载.验证.准备.解析.初始化.使用.卸载 7个阶段,其中验证.准备.解析 3个部分统称为 连接. 二.具体步骤 ...

  8. Python学习笔记(三): 收集参数

    如下代码: >>>def print_params(title,*params) print title print params >>>print_params( ...

  9. KBEngine游戏服务器(一)——引擎环境配置

    系统:Win10 版本:Visual Studio 2013(也就是vs120) kbengine:v1.0.0 MySQL:5.7 MySQL Workbench :6.3 一.下载kbengine ...

  10. NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.view.InternalResourceViewResolver' available

    问题描述: 项目中需要配置多个视图解析器,所以使用ContentNegotiatingViewResolver来处理,在其内部设置了FreeMarkerViewResolver .InternalRe ...