HDU - 1973 - Prime Path (BFS)
Prime Path
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 987 Accepted Submission(s): 635
— 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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} bool isprime[maxn];
int step[maxn];
bool vis[maxn];
int num1,num2;
void getprime(int n)
{
for(int i=;i<=n;i++)
isprime[i]=true;
isprime[]=isprime[]=false;
for(int i=;i<=n;i++)
{
for(int j=*i;j<=n;j=j+i)
isprime[j]=false;
}
} int bfs(int st )
{ vis[st]=true;
step[st]=;
queue<int>que;
que.push(st);
while(que.size())
{
int p=que.front();
que.pop();
if(p == num2)
{
return step[num2];
break;
}
int ge=p % ;
int shi=(p/)%;
int bai=(p/)%;
int qian=p/;
for(int i=;i<=;i++)
{
int next;
if(i!=ge)
{
next=p-ge+i;
if(next>= && next<= && isprime[next] && vis[next]==false)
{
step[next]=step[p]+;
que.push(next);
vis[next]=true;
// cout<<"ge";
}
}
if(i!=shi)
{
next=p-shi*+i*;
if(next>= && next<= && isprime[next] && vis[next]==false)
{
step[next]=step[p]+;
que.push(next);
vis[next]=true;
// cout<<"shi";
}
}
if(i!=bai)
{
next=p-bai*+i*;
if(next>= && next<= && isprime[next] && vis[next]==false)
{
step[next]=step[p]+;
que.push(next);
vis[next]=true;
// cout<<"bai";
}
}
if(i!=qian)
{
next=p-qian*+i*;
if(next>= && next<= && isprime[next] && vis[next]==false)
{
step[next]=step[p]+;
que.push(next);
vis[next]=true;
// cout<<"qian";
}
}
} }
return -;
}
int main()
{
int t;
scanf("%d",&t);
getprime();
while(t--)
{
memset(vis,false,sizeof(vis));
memset(step,,sizeof(step));
scanf("%d %d",&num1,&num2); // for(int i=2;i<=9999;i++)
// if(isprime[i])
// cout<<i<<" ";
int ans=bfs(num1);
if(ans>=)
printf("%d\n",ans);
else
printf("Impossible\n");
}
}
HDU - 1973 - Prime Path (BFS)的更多相关文章
- 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也是四位的素数时才能进行这种改变.请你计算 ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- 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 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- 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 ...
随机推荐
- ajax post方式表单提交的注意事项。
当我们创建一个异步对象XMLHttpRequest同时post方式向后台传输数据的时候. 我们要设置异步对象的xhr.setRequestHeader成员的值为 XMLHttpRequest.setR ...
- ElasticSearch 处理自然语言流程
ES处理人类语言 ElasticSearch提供了很多的语言分析器,这些分析器承担以下四种角色: 文本拆分为单词 The quick brown foxes → [ The, quick, brown ...
- Teradata 认证系列 - 3. 关系型数据库的概念
本课的学习目标 定义关系型数据库关联的术语 讨论主键的功能 讨论外键的功能 列出关系型数据库的优势 描述星型架构和第三范式数据模型的区别 什么是数据库?数据库是一个应用永久保存数据的集合表现在: 逻辑 ...
- border实现矩形中斜线分割 切换按钮
思路:将该矩形分为三个div,中间的div使用border的特性 代码实现如下:
- 【硬盘整理】使用UltimateDefrag将常用文件放置在磁盘最外圈
使用方法未知.软件截图如下: 官方网站(英文):http://www.disktrix.com/ 汉化破解版V3.0下载地址:http://page2.dfpan.com/fs/7com9monca3 ...
- 关于前端的交互 ajax
对于交互来说,可以利用原生的javascript和jquery 这篇说的就是jquery 1 不是跨域的 利用$ajax({})这个函数实现的 $.ajax({ url: "", ...
- js 中的 Math.ceil() Math.floor Math.round()
alert(Math.ceil(25.9)); alert(Math.ceil(25.5)); alert(Math.ceil(25.1)); alert(Math.round(25.9)); ale ...
- IOS 长按+轻扫(手势识别)
@interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...
- iOS 3DTouch 的开发套路大全
今天休息,先来无事就自己看了一下,关于3d touch,看完后自己有动手尝试了一下,感觉也并不是很难, 经过大量的搜索,目前为止,发现有三种实现方式: 第一种:info文件中添加字段 这种方式,最为直 ...
- python_77_json与pickle序列化3
#此方法:dump多次,而不可以load多次,只能load一次,否则会出错 只有序列化,无反序列化 import json info={ 'name':'Xue Jingjie', 'age':22, ...