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 ...
随机推荐
- 基于nginx的FastCGI的缓存配置
废话不多说了, 直接上配置, 其实 fastcgi_cache 和 proxy_cache 的配置基本一样: # !缓存文件存放目录 # levels 缓存层次 # keys_zone 缓存空间名和共 ...
- sql server 分析
查询指令,查询数据库的版本 SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPE ...
- ForkJoin有参无返回值、有参有返回值实例
介绍: a . Fork/Join为JKD1.7引入,适用于对大量数据进行拆分成多个小任务进行计算的框架,最后把所有小任务的结果汇总合并得到最终的结果 b . 相关类 public abstract ...
- 服务器部署nginx报错 nginx: [warn] conflicting server name "localhost" on xxx.xxx.xxx.xxx:80, ignored
问题 修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后, ...
- vuex文档(附加个人理解)
Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到Vue 的 ...
- logback的加载过程
使用logback-classic.jar时,启动应用后,logback按照以下顺序进行扫描: 1.在系统配置文件System Properties中寻找是否有logback.configuratio ...
- 关闭VAX的拼写检查_解决中文红色警告问题
菜单VAssistX->Visual Assistant X Options->Advanced->Underlines下 取消“Underline spelling errors ...
- Piwik-2.16.1 (OpenLogic CentOS7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: centos7.2 piwik devops log analysis monitoring open-source 服务优惠价: 按服务商许可协议 ...
- GBase数据库存储过程——批量查询多个数据表的磁盘占用情况
--清理历史表,可选 DROP TABLE IF EXISTS `dap_model`.`data_statics`; CREATE TABLE `dba`.`data_statics` ( `TAB ...
- shell实现网站备份
#!/bin/bash ##back web directory scripts #需要备份的目录写入与脚本同级目录test.txt文件中 DIR="/data/server/www&quo ...