poj3216 Prime Path(BFS)
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
Output
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=;i*i<=a;i++)
if(a%i==)return false;
return a!=;
}
queue<niu>q;
bool vis[];
int bfs()
{
memset(vis,false,sizeof(vis));
while(q.size())q.pop();
q.push(niu(a,));
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%;
int cur=(tmp.prime/)%;
for(int i=;i<=;i++)
{
int nx=(tmp.prime/)*+i;
if(check(nx)&&!vis[nx])
{
vis[nx]=true;
q.push(niu(nx,tmp.step+));
}
int ny=(tmp.prime/)*+i*+cnt;
if(check(ny)&&!vis[ny])
{
vis[ny]=true;
q.push(niu(ny,tmp.step+));
}
int nz=(tmp.prime/)*+i*+cur*+cnt;
if(check(nz)&&!vis[nz])
{
vis[nz]=true;
q.push(niu(nz,tmp.step+));
}
if(i==)continue;
int nn=(tmp.prime%)+i*;//cout<<nn<<endl;
if(check(nn)&&!vis[nn])
{
vis[nn]=true;
q.push(niu(nn,tmp.step+));
}
}
}
return ans==INF?-:ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ans=INF;//注意这里的ans要初始化
cin>>a>>b;
if(bfs()==-)
cout<<"Impossible"<<endl;
else cout<<bfs()<<endl;
}
return ;
poj3216 Prime Path(BFS)的更多相关文章
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ-3126-Prime Path(BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27852 Accepted: 15204 Desc ...
随机推荐
- ViewMode
一.ViewMode 实现使用场景-Model枚举的情景下, 注意:枚举声明在后台的时候,需要渲染界面,页面表格使用 Bootstrap Table插件-事先通过ajax 渲染(数据库读取值1.2.3 ...
- openstack--all-in-one部署
安装过程 本次宿主机(即安装OpenStack的机器)的操作系统是CentOS 7.5.安装的OpenStack是目前最新的rocky版本,官方文档建议机器至少有16 GB的内存,处理器硬件虚拟化扩展 ...
- mongodb 多表关联处理 : 内嵌以及连接(手动引用、DBref) 、aggregate中$lookup
MongoDB与关系型数据库的建模还是有许多不同,因为MongoDB支持内嵌对象和数组类型.MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).那么何时Embed何时Li ...
- windows2008R2-Exchange管理笔记
命令全在Exchange shell里面执行 批量修改用户属性 Set-User -Identity liganwei@yjcn.com -Phone "分机" -HomePhon ...
- 【leetcode】572. Subtree of Another Tree
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- ldd3 第12章 PCI驱动程序
PCI接口 PCI寻址 引导阶段 配置寄存器和初始化 MODULE_DEVICE_TABLE 注册PCI驱动程序 佬式PCI探测 激活PCI设备 访问配置空间 访问I/O和内存空间 PCI中断 硬件抽 ...
- paper 159:文章解读:From Facial Parts Responses to Face Detection: A Deep Learning Approach--2015ICCV
文章链接:https://arxiv.org/pdf/1509.06451.pdf 1.关于人脸检测的一些小小总结(Face Detection by Literature) (1)Multi-vie ...
- (转)JVM运行时数据区
转:http://www.cnblogs.com/myna/p/7567208.html java虚拟机运行时数据区,具体分为如下几个区域 程序计数器(Program Counter Register ...
- css如何让<a>标签,根据输入的内容长度调整宽度,宽度自适应,那位大仙帮帮忙
css如何让<a>标签,根据输入的内容长度调整宽度,宽度自适应,那位大仙帮帮忙 5 样式 .ceshi{float:left; margin-left:13px; width:180px; ...
- DomainObjectUtility
using System; using System.Collections; using System.Collections.Generic; using System.Collections.S ...