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 ...
随机推荐
- SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换
SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...
- R语言控制流
一般来说R语言是自上而下执行的,但是遇到特殊情况可能用到循环执行某些语句,这时候条件运算和循环就能派上用场了.
- How To Find Out Attachments By File Type In Outlook?
ext: (extension extension) Take the attachments of zip files and of txt files for example, just ente ...
- 六、实现一个小功能 todolist
1.创建一个新的Compnent 如果是通过 cli 创建的会自动加入,如果是手动创建的,需要自己加入. 2.实现添加效果 3.实现删除按钮 4.优化,把点击 添加 改为 回车 添加 5.优化,分成“ ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- AI比医生更好地发现皮肤癌,未来计算机技术可渗透医院
未来机器人将取代医生?这可能是事实.为什么这么多年轻人选择计算机行业,因为这是一个趋势.据法新社报道,研究人员周二称,一项计算机技术比人类皮肤科医生在检测皮肤癌方面的表现要好得多,因为这项研究是为了寻 ...
- Java Web 之HttpServletRequest对象初识
通过request对象获得请求行 获得客户端请求方式:String getMethod(); 获得请求的资源: String getRequestURL(); String getQueryStrin ...
- java获取当前月第一天和最后一天
获取当前月第一天: /** * 获取当前月第一天 * @param month * @return */ public static String getFirstDayOfMonth(int mon ...
- shell脚本学习(5)join
join 不是简单的把两个文本连接起来 sale.txt quotas.txt
- JS中数据结构之集合
集合(set)是一种包含不同元素的数据结构.集合中的元素称为成员.集合的两个最重要特性是:首先,集合中的成员是无序的:其次,集合中不允许相同成员存在.当你想要创建一个数据结构用来保存一些独一无二的元素 ...