题目传送门

 Prime Path

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033
1733
3733
3739
3779
8779
8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output
6
7
0题意:给你四位数字a,b;每一不只能改变一位数字,且新的数字只能是素数,要你输出最小步数

题解:bfs,每次向下遍历40个方向

代码:
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
struct niu
{int prime,step;niu(){}niu(int pr,int st){prime=pr,step=st;}
};
int a,b;
int ans=INF;
bool check(int a)
{for(int i=2;i*i<=a;i++)if(a%i==0)return false;return a!=1;
}
queue<niu>q;
bool vis[10005];
int bfs()
{memset(vis,false,sizeof(vis));while(q.size())q.pop();q.push(niu(a,0));vis[a]=true;while(q.size()){niu tmp=q.front();q.pop();//cout<<tmp.prime<<tmp.step<<endl;if(tmp.prime==b){ans=min(ans,tmp.step);}int cnt=tmp.prime%10;int cur=(tmp.prime/10)%10;for(int i=0;i<=9;i++){int nx=(tmp.prime/10)*10+i;if(check(nx)&&!vis[nx]){vis[nx]=true;q.push(niu(nx,tmp.step+1));}int ny=(tmp.prime/100)*100+i*10+cnt;if(check(ny)&&!vis[ny]){vis[ny]=true;q.push(niu(ny,tmp.step+1));}int nz=(tmp.prime/1000)*1000+i*100+cur*10+cnt;if(check(nz)&&!vis[nz]){vis[nz]=true;q.push(niu(nz,tmp.step+1));}if(i==0)continue;int nn=(tmp.prime%1000)+i*1000;//cout<<nn<<endl;if(check(nn)&&!vis[nn]){vis[nn]=true;q.push(niu(nn,tmp.step+1));}}}return ans==INF?-1:ans;
}
int main()
{int T;cin>>T;while(T--){ans=INF;//注意这里的ans要初始化cin>>a>>b;if(bfs()==-1)cout<<"Impossible"<<endl;else cout<<bfs()<<endl;}return 0;

转载于:https://www.cnblogs.com/zhgyki/p/9505027.html

poj3216 Prime Path(BFS)相关推荐

  1. Prime Path(bfs)广度优先搜索

    题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

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

    题干: 给你两个四位的素数a,b. a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变. 请你计算a最少经过多少次上述变换才能变成b. 例如:1033 -> 8179  ...

  3. POJ 3126 Prime Path(BFS + 素数打表)

    题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...

  4. POJ - 3126 - Prime Path(BFS)

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

  5. (BFS)Prime Path (poj3126)

    题目: 孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了.zydsg要做些改变,他想去和素数小姐姐约会. 所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这 ...

  6. 《算法笔记》学习日记——8.2 广度优先搜索(BFS)

    目录 8.2 广度优先搜索(BFS) 问题 A: Jugs 问题 B: DFS or BFS? 问题 C: [宽搜入门]8数码难题 问题 D: [宽搜入门]魔板 问题 E: [宽搜入门]巧妙取量 小结 ...

  7. 广度优先搜索(BFS)与深度优先搜索(DFS)

    一.广度优先搜索(BFS) 1.二叉树代码 # 实现一个二叉树 class TreeNode:def __init__(self, x):self.val = xself.left = Nonesel ...

  8. 深搜(DFS)和宽搜(BFS)

    相同点: 深搜和宽搜都可以对空间进行遍历,搜索的结构都是树 不同点: 深搜(DFS):(暴搜)(直男)(执着的人) (1)尽可能往深了搜,当搜到叶节点(简称搜到头)就会回溯,然后再搜下一个,然后再回溯 ...

  9. python扫雷 广度优先_Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)...

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

最新文章

  1. Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
  2. css画横线箭头_用CSS绘制三角形箭头
  3. LUA脚本调用C场景,使用C API访问脚本构造的表
  4. 成功解决RuntimeWarning: divide by zero encountered in double_scalars
  5. 计算机学院迎新晚会集宁,迎新晚会 | 信息管理学院2017年“海姆达尔之眼”迎新晚会圆满成功...
  6. 安装docker遇到的坑 Could not resolve host: download.docker.com;
  7. CF1060C Maximum Subrectangle
  8. centos7-每天定时备份 mysql数据库
  9. 安装inotify-tools监控工具
  10. Web 前端编程运维必备
  11. Win10开机免UAC自启动锐捷客户端
  12. 将 SVN Repositories 内部的下层目录,提升成为顶层 Repositories 的风险
  13. 随机游走模型 matlab,随机游走matlab程序
  14. 2021CCPC女生专场
  15. 实现简易版vue2(数据劫持,观察者,发布订阅)
  16. ASCII 编码对照表 一览表
  17. 【大数据】服务器硬盘基础知识
  18. A fast surrogate-assisted PSO algorithm for computationally expensive proble 阅读笔记
  19. 本科、硕士、博士的区别(终极版)
  20. 100天精通Oracle-实战系列(第8天)保姆级 PL/SQL Developer 安装与配置

热门文章

  1. Java抽象类的概念和使用
  2. Tensorflow Lite 编译
  3. gRPC的那些事 - streaming
  4. Tensorflow学习
  5. python的误差条形图的解释_如何使用python绘制带有误差条的条形图?
  6. c#如何通过ftp上传文件_ftp自动上传工具,ftp自动上传工具如何自动上传文件
  7. NOIP信息奥赛--1995“同创杯”初中复赛题题解(四)
  8. vuex mysql_vuex + koa + mysql实现购物车功能(一)
  9. cnc加工中心保养表_CNC加工中心有哪些日常保养方法?
  10. ICCV 2019 运行LCGN遇到的问题及解决办法