poj 3126 Prime Path(搜索专题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20237 | Accepted: 11282 |
Description

— 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
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; bool prime[];
int que[];
int step[];
int isprime[];
bool vis[];
int cou=; void getprime(){
int num=;
num=sqrt(num*1.0);
memset(prime,true,sizeof(prime));
prime[]=prime[]=false;
for(int i=;i<;i+=){
prime[i]=false;
}
for(int i=;i<num;i+=){
if(prime[i]){
for(int j=i*i;j<;j+=*i){
prime[j]=false;
}
}
}
for(int i=;i<;i++){
if(prime[i]){
isprime[cou++]=i;
}
}
} bool judge(int a,int b){
int sum=;
int t1[],t2[];
for(int i=;i<;i++){
t1[i]=a%;
a/=;
t2[i]=b%;
b/=;
}
for(int i=;i<;i++){
if(t1[i]==t2[i]){
sum++;
}
}
if(sum==){
return true;
}else{
return false;
}
} int bfs(int a,int b){
int start=;
int endd=;
memset(vis,true,sizeof(vis));
que[endd]=a;
step[endd++]=;
while(start<endd){
int now=que[start];
int nowStep=step[start++];
if(now==b){
return nowStep;
}
for(int j=;j<=cou;j++){
int i=isprime[j];
if(!vis[i]){
continue;
}
if(prime[i]&&judge(i,now)){
if(i==b){
return nowStep+;
}
que[endd]=i;
step[endd++]=nowStep+;
vis[i]=false;
}
}
}
return ;
} int main()
{
int n;
scanf("%d",&n);
int a,b;
getprime();
for(int i=;i<n;i++){
scanf("%d %d",&a,&b);
int ans=bfs(a,b);
printf("%d\n",ans);
}
return ;
}
poj 3126 Prime Path(搜索专题)的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 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: 26475 Accepted: 14555 Descript ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- (简单) 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 The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
随机推荐
- [转载]理解 Git 分支管理最佳实践
原文 理解 Git 分支管理最佳实践 Git 分支有哪些 在进行分支管理讲解之前,我们先来对分支进行一个简单的分类,并明确每一类分支的用途. 分支分类 根据生命周期区分 主分支:master,deve ...
- Oracle创建Database Link
一菜单方式: 打开plsql,点击[File]-[New]-[Database link],打开如下图所示窗口 填好各项信息后,点击[Apply]即可完成Database Link的创建. 二SQL方 ...
- 安装Harbor
一.安装Harbor 1. Harbor简介 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Dock ...
- Ubuntu安装最新版nodejs
今天在学习以太坊时,需要用到nodejs,因为使用的是ubuntu 16.04 LTS,一直安装的是老版本的nodejs,官方给方法用不成,折腾了半天,什么软链.手动编译,总觉得不很靠谱(linux水 ...
- shell编程学习笔记(五):Shell中脚本的参数
在执行Shell脚本的时候,可以在执行时带上参数,相当于传递参数给脚本,下面我们看一下怎么使用这个参数 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/script ...
- Sandcastle Help File Builder(.NET帮助文档工具)的版本选择心得——支持VS2010至VS2015,高版本项目文件问题
作者: zyl910 一.缘由 "Sandcastle Help File Builder"(简称SHFB)是一个很好用.NET 帮助文档生成工具. 但它的每个版本支持的VS版本范 ...
- SpUtil多样加密存储,兼容android9.0
代码地址如下:http://www.demodashi.com/demo/15058.html 前言 在android系统不断升级的过程中,Sharepreferences存储出现多中问题,其中有些是 ...
- git上传本地Intellij idea 项目到码云的git仓库中
.安装git客户端 Window下安装git客户端. 二.配置Intellij idea中的Git/ GitHub 打开Preference-- Version Control. 下拉选择Github ...
- 转:一千行MYSQL 笔记
原文地址: https://shockerli.net/post/1000-line-mysql-note/ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ ...
- Python(字符编码)
https://www.cnblogs.com/zihe/p/6993891.html 一 了解字符编码的知识储备 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开 ...