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 ...
随机推荐
- 斗鱼扩展初识Chrome扩展(一)
看斗鱼有些时间了,也写了不少辅助的js,但是昨天不小心把硬盘分区表搞没了,自己写了好久的代码不见了,DiskGenius 也没恢复成功,所以要重写一次,大家要引以为鉴,常备份代码,github是个不错 ...
- 一个关于document.write()的问题
Index.html中: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- js函数获取ev对象
今天工作中遇到一个问题,就是平时获取一个ev(event)对象时候一般直接在调用方法里面写一个ev参数,即可直接拿到这个对象,但是有时候会遇到函数调用不是直接加在一个dom对象的后面,如: var o ...
- Eucalyptus-NC管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- git版本分支和分支、分支和主分支切换
问题描述: 公司里项目管理使用的是gitLab(收费的), 如果开发人员提交代码, 需要首先创建一个分支, 然后把代码提交到你创建的分支上去(不允许把代码直接提交到主分支上). 在代码提交到已经创建 ...
- C#开发android应用实战 源码
原书名: Professional Android Programming with Mono for Android and .NET/C# Download Title Size Down ...
- 报错:Program bash is not found in PATH
(如果按照我的方法来的话是没有这个错误的,我之前用别的方法的时候有但是后来还是没解决,写出来放到这里做参考吧) 参考原文:http://blog.csdn.net/fuyongbing1986/art ...
- 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问
一.memcached是什么? 二.memcached不互相通信的分布式 三.安装步骤 四.本文介绍的命令主要包括: 存入命令(Storage commands) 取回命令(Retrieval com ...
- 六、C++离散傅里叶逆变换
C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...
- JavaScript-判断语句(if...else)
语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时执行的代码 } 假设我们通过年龄来判断是否为成年人,如年龄大于等于18岁,是成年人,否则不是成年人.代码表示如下: < ...