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 ...
随机推荐
- CSS魔法堂:改变单选框颜色就这么吹毛求疵!
前言 是否曾经被业务提出"能改改这个单选框的颜色吧!让它和主题颜色搭配一下吧!",然后苦于原生不支持换颜色,最后被迫自己手撸一个凑合使用.若抛开input[type=radio] ...
- Linux之网络编程:时间服务器
基于TCP-服务器 1,创建一个socket套接字 int socket(int domain,int type,int protocol) domain:IP地址族,AF_INET(IPv4).AF ...
- Zip文件和RAR文件解压
直接上工具类: package com.ksource.pwlp.util; import java.io.File; import java.io.FileOutputStream; import ...
- ninja-build环境安装
ninja是一个小型构建系统,专注于速度,和常用的make类似,有一些软件就是基于ninja编译构建的,比如clickhouse数据库就需要依赖ninja,因为最近在研究clickhouse,需要依赖 ...
- 阿里云物联网平台体验(NetGadgeteer+C#篇)
目前对接阿里云物联网平台有多种软件和硬件,可以有多种不同语言来实现对接,比如c/c++,Java,JS,Python,C#等等,不过C#版本只有PC对接云平台的代码,嵌入式的目前还没有看到,所以本篇文 ...
- (原)Max Area of Island(即连通域标记)
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10493114.html 1. 问题 Given a non-empty 2D array grid ...
- sql 有条件计数
select InstitutionID=LEFT(InstitutionID,9), Irregularities_Type=sum(CASE WHEN Irregularities_Type> ...
- Docker入门 - 001 CentOS Docker 安装
Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发行版本中的内核支持 Docker. ...
- IOS-企业开发人员账号&邓白氏码申请记录
Apple开发人员账号分三种,个人.公司,还有企业.个人和公司都称为标准账号. 另一种是教育机构的账号. 账号介绍 个人和公司的就不说了.如今仅仅说企业账号 首先是申请企业账号的地址: https:/ ...
- HIVE metastore Duplicate key name 'PCS_STATS_IDX' (state=42000,code=1061)
HDP 版本:2.4.0.0-169. 解决:将hive 所在 节点上的/usr/hdp/2.4.0.0-169/hive/script/metastore/upgrade/msql/hive-sch ...