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 ...
随机推荐
- [Windows] 一些简单的CMD命令
开始菜单中的“运行”是通向程序的快捷途径,输入特定的命令后,即可快速的打开Windows搜索的大部分程序,熟练的运用它,将给我们的操作带来诸多便捷. winver 检查Windows版本 wmimgm ...
- 《C#高效编程》读书笔记05-为类型提供ToString()方法
System.Object.ToString()是.NET环境中最常用的方法之一.编写类型时,要提供一个合理的ToString版本,否则使用者就不得不自己构造一套可以阅读的表示. public cla ...
- Devexpress之GridControl显示序列号
先上图: 操作方法: 1.先设置一下gridview中属性:IndicatorWidth,一般为:40.如下图:(一般可以显示5位数字.如要更长显示,自己测试一下.) 2.找到gridview中的:C ...
- 关于小程序后台post不到数据的问题
小程序post请求获取不到数据问题 把headers的参数“Content-Type”的值改为application/x-www-form-urlencoded: Request Body Type ...
- mysql通过event和存储过程实时更新简单Demo
今天想稍微了解一下存储过程和EVENT事件,最好的方法还是直接做一个简单的demo吧 首先可以在mysql表中创建一个users表 除了设置一些username,password等必要字段以外还要设立 ...
- python基础-字符串操作
输出高亮 语法: 显示方式.前景色.背景色至少一个存在即可. 显示方式:0(关闭所有效果),1(高亮),4(下划线),5(闪烁),7(反色),8(不可见). 前景色以3开头,背景色以4开头,具体颜 ...
- Azure School,让系统化学习回归一站式的简单体验
承认吧,「终身制学习」已经成为一个不可抵挡的趋势.不管你从事什么行业,几乎已经没有什么可以一直吃老本就能搞定的事情,总有各种新的技术和概念等着你去学.至于发展速度飞快的IT 技术,不断的学习更是贯彻始 ...
- pysnmp程序
功能 访问远程交换机snmp数据,写入本地influxdb数据库 #!/usr/bin/env python # -*- encoding: utf-8 -*- import os, yaml, ti ...
- spring中用xml配置构造注入的心得
spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...
- 小记:vue 及 react 的工程项目入口小结及 webpack 配置多页面应用参考
一.前言 闲暇时间,看了下前端的基础知识,有幸参与了公司公众号项目前面的一个阶段,学习到了一些前端框架的相关知识 小结了一下 自己练习通过新建一个个文件组织起项目的过程中的一些理解 二.项目入口 vu ...