Prime Path

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 987    Accepted Submission(s): 635

Problem Description
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. 
— 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
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
 
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
 
Sample Input
3
1033 8179
1373 8017
1033 1033
 
Sample Output
6
7
0
 
Source
 
Recommend
wangye
 
 
题意:给你两个四位数,都是素数,每次改变可以改变其中的任何一个数字,但要求改变后的四位数(没有前导零)依然是素数,问最少改变几次可以使得第一个数改为第二个数
思路:可以先用埃氏筛法把1000-9999的所有的素数都选出来,之后就四个位数,每个位数最多改变八次,就搜索一下,我开了isprime和step两个数组,当然也可以开一个结构体,但只改变一个数字需要花点功夫,我是枚举了个位,十位,百位,千位。
 
#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)的更多相关文章

  1. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  2. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  3. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  4. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  5. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  6. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  7. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  8. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  9. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

随机推荐

  1. android 开发-系统设置界面的实现

    具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...

  2. 将MySQL转化为mysqli

    <?php/** * Created by PhpStorm. * User: 大神 * Date: 2017/7/24 * Time: 11:29 */ header('content-typ ...

  3. CentOS yum安装mcrypt

    CentOS yum安装mcrypt   本篇排错的前提是只想用yum安装,不想使用源码包编译安装. php依赖一下包:   #yum install libmcrypt libmcrypt-deve ...

  4. 《zabbix监控的搭建》centos5.8 32

    系统环境centos5.8 32位操作系统   这里以zabbix-2.2.7为例: 下载官方的软件包: http://pan.baidu.com/s/1ntuTRYh 官方的参考文档: https: ...

  5. win10命令行压缩zip文件

    1.下载winzip,下载winzip command line(官方) 2.使用命令 "C:\Program Files\WinZip\WZZIP.exe" C:\test\a. ...

  6. 现代java开发指南系列

    [翻译]现代java开发指南系列 [翻译]现代java开发指南 第一部分 [翻译]现代java开发指南 第二部分 [翻译]现代java开发指南 第三部分

  7. 【问题记录】mysql TIMEDIFF 和 TIMESTAMPDIFF的使用

    今天遇到一个需求,需要计算数据表中两个时间的差值,并取对应的秒数 一开始我是用 time_to_sec(timediff (time1,time2)) 但是这样会有一个问题,,,时间短的用这个计算没有 ...

  8. 7天学完Java基础之0/7

    笔记-7天学完Java基础之0/7 1.常用命令提示符(cmd) 启动:Win+R,输入cmd​

  9. springboot 学习笔记(五)

    (五)springboot整合thymeleaf模板,实现简单的登陆 1.修改上一节笔记中的user表,新增一个password字段,同时要求username为UNIQUE,以实现登陆校验,表结构如下 ...

  10. hibernate自动建表技术_采用数据库反向生成技术

    1.首先使用oracle创建一个用户: 登陆sqlplus,并以sysdba登陆到数据库: 2.创建一个用户,并对此用户授予connect,resource两个角色的权限: 3.连接到hibernat ...