题目链接:http://poj.org/problem?id=3126

解题报告:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h> using namespace std; #define MAXV 10000 bool Prime[MAXV]; ///素数表
bool vis[MAXV]; ///素数是否访问
int cnt[MAXV]; ///cnt[i]表示变换到素数i时,变换的次数,答案及cnt[last]; void init() { ///对素数打表
int i,j;
for(i=; i<=MAXV; i++) {
for(j=; j<i; j++)
if(i%j==) {
Prime[i]=false;
break;
}
if(j==i) Prime[i]=true;
}
} int bfs(int first,int last) {
queue <int>q;
int vtemp,t[];
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt)); q.push(first);
vis[first]=true; while(!q.empty()) {
int v=q.front();
q.pop(); t[]=v/;
t[]=v%/;
t[]=v%/;
t[]=v%; for(int j=; j<; j++) {
int temp=t[j];
for(int i=; i<; i++) {
if(i!=temp) {
t[j]=i;
vtemp=t[]*+t[]*+t[]*+t[];
if(!vis[vtemp] && Prime[vtemp]) {
cnt[vtemp]=cnt[v]+;
vis[vtemp]=true;
q.push(vtemp);
}
if(vtemp==last) return cnt[vtemp];
}
t[j]=temp;
}
}
if(v==last) return cnt[v];
}
return -;
} int main() {
int n,a,b,key;
init();
scanf("%d",&n);
while(n--) {
scanf("%d%d",&a,&b);
key=bfs(a,b);
if(key!=-) printf("%d\n",key);
else printf("Impossible\n");
}
return ;
}

BFS变换素数,POJ(3126)的更多相关文章

  1. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  2. POJ - 3126 - Prime Path(BFS)

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

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

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

  4. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  5. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  8. POJ - 3126 bfs + 素数筛法 [kuangbin带你飞]专题一

    题意:给定两个四位素数作为终点和起点,每次可以改变起点数的某一位,且改变后的数仍然是素数,问是否可能变换成终点数字? 思路:bfs搜索,每次改变四位数中的某一位.素数打表方便判断新生成的数是否是素数. ...

  9. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

随机推荐

  1. js打印相关,注意此方法受到IE安全性设置影响

    <HTML><HEAD><TITLE>javascript打印-打印页面设置-打印预览代码</TITLE>  <SCRIPT language=j ...

  2. ajaxfileupload.js异步上传

    转载:https://www.cnblogs.com/labimeilexin/p/6742647.html jQuery插件之ajaxFileUpload     ajaxFileUpload.js ...

  3. idea生成springBoot 报错403

    问题: idea创建springboot失败 Initialization failed Cannot download 'https://start.spring.io': Status: 403 ...

  4. inode与block知识总结

    inode概述:硬盘要分区,然后格式化,创建文件系统在每个Linux存储设备的分区被格式化为ext3文件系统后一般有两个部分:    第一部分Inode:存储这些数据的属性信息(大小,属主,归属的用户 ...

  5. maya2013安装失败如何卸载重装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  6. Unity 组件.name

    组件.name  指的是组件所在游戏对象的名字,例如: Animation m_animation; m_animation =GetComponent<Animation>(); m_a ...

  7. 数据库mysql基础语言--各模式的含义

    1. 欢迎信息 欢迎来到 MySQL 镜像.命令以 ; 或 g 结束.你的 MySQL 连接编号为 2.服务器版本:5.1.47-社区 MySQL 社区服务器(GPL) 版权(C)2000.2010, ...

  8. 分布式任务框架elastic-job 学习笔记

    官方资料:https://github.com/dangdangdotcom/elastic-job ------------------------------------------------- ...

  9. 使用Zxing生成一维码和二维码

    首先引用zxing.dll 到项目中引用 using System; using System.Collections.Generic; using System.Drawing; using Sys ...

  10. js弹出页面

    建立一个HTML文件,输入以下代码就能弹出页面 <!DOCTYPE html> <html lang="en"> <head> <meta ...