Prime Path(BFS)
Prime Path
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 18

— 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<cstring>
- using namespace std;
- int a,b;
- struct kf
- {
- int number;
- int sgin;
- }ks[];
- bool ksgin[]={false};
- bool shu(int sg)//判断sg是否是素数
- {
- if(sg==||sg==)
- return true;
- else if(sg<=||sg%==)
- return false;
- else if(sg>)
- {
- for(int i=;i*i<=sg;i+=)
- if(sg%i==)
- return false;
- return true;
- }
- }
- int bfs()
- {
- int left,right;
- kf s;
- ks[left=right=].number=a;
- ks[right++].sgin=;
- ksgin[a]=false;
- while(left<right){
- s=ks[left++];
- if(s.number==b){
- cout<<s.sgin<<endl;
- return ;
- }
- int ge=s.number%;
- int shi=(s.number/)%;
- for(int i=;i<=;i+=){//枚举个位
- int y=s.number/*+i;
- if(y!=s.number&&ksgin[y]&&shu(y)){
- ksgin[y]=false;
- ks[right].number=y;
- ks[right++].sgin=s.sgin+;
- }
- }
- for(int i=;i<=;i++){//枚举十位
- int y=s.number/*+i*+ge;
- if(y!=s.number&&ksgin[y]&&shu(y)){
- ksgin[y]=false;
- ks[right].number=y;
- ks[right++].sgin=s.sgin+;
- }
- }
- shi*=;
- shi+=ge;
- for(int i=;i<=;i++){//枚举百位
- int y=s.number/*+i*+shi;
- if(y!=s.number&&ksgin[y]&&shu(y)){
- ksgin[y]=false;
- ks[right].number=y;
- ks[right++].sgin=s.sgin+;
- }
- }
- shi=s.number%;
- for(int i=;i<=;i++){//千位
- int y=i*+shi;
- if(y!=s.number&&ksgin[y]&&shu(y)){
- ksgin[y]=false;
- ks[right].number=y;
- ks[right++].sgin=s.sgin+;
- }
- }
- }
- cout<<"Impossible"<<endl;
- return ;
- }
- int main()
- {
- // freopen("input.txt","r",stdin);
- int test;
- cin>>test;
- while(test--){
- memset(ksgin,true,sizeof(ksgin));
- cin>>a>>b;
- bfs();
- }
- }
Prime Path(BFS)的更多相关文章
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【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 ...
- 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 ...
- 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: 27852 Accepted: 15204 Desc ...
随机推荐
- PhpMyAdmin隐藏数据库设置同前缀失效的问题
用PhpMyAdmin默认会把所有数据库都显示出来,一些如 MySQL,information_schema之类的也会显示,这样既不安全看着也不爽,隐藏掉最好. 修改 config.inc.php 或 ...
- CodeForces 707A Brain's Photos
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- 史上最详细SharePoint 2013安装步骤图解新手教程
来源:// http://www.itexamprep.com/cn/microsoft/soft/sharepoint2013/2013/0408/2866.html 文章就是SharePoint2 ...
- ubuntu开放防火墙端口
root@jbxue:$ sudo ufw enable Firewall started and enabled on system startup root@jbxue:$ sudo ufw ...
- java中equals方法和contentEquals方法区别
java中,String类里提供了两种字符串的比较方式(算上“==”应该是三种) String line1 = new String("0123456789"); String l ...
- java 学习心得
- SQL 小笔记
如何得到字段的类型 select sql_variant_property(ID,'BaseType') from tb
- centos7,yum安装的redis用systemctl无法启动
因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...
- one-sided limit
Limit[e^(-1/x),x->0,Direction->-1] means $\lim_{x \to 0^{+}}e^{-\frac{1}{x}}$ Limit[e^(-1/x),x ...
- 表格单元格td设置宽度无效的解决办法 .
http://zzstudy.offcn.com/archives/11366 在做table页面时,有时对td设置的宽度是无效的,td的宽度始终有内部的内容撑开,可以设置padding,但直接设置w ...