poj3126 Prime Path BFS
Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26158 | Accepted: 14376 |
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
题意:
给定两个素数a b,求a变到b需要几步,并且变换时只有一个数字不同,并且是素数
思路:
因为是四位的素数,所以先对素数打表,然后BFS,每次进入队列的是:只有一个数字不同的四位数,并且是素数,就入队
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<cstdio>
using namespace std;
#define MAXV 10009bool prime[MAXV];void init(){ //对素数打表int i,j;for(i=1000;i<=MAXV;i++){for(j=2;j<i;j++)if(i%j==0){prime[i]=false;break;}if(j==i) prime[i]=true;}/*memset(prime,0,sizeof(prime));int m=(int)sqrt(MAXV+0.5);for(int i=2;i<=m;i++)if(!prime[i])for(int j=i*i;j<MAXV;j+=i)prime[j]=1;*/
}
int bfs(int first,int last){bool dis[MAXV];queue <int>q;int v,i,j,temp,vtemp,count[MAXV],t[4];memset(dis,false,sizeof(dis));memset(count,0,sizeof(count));q.push(first);dis[first]=true;while(!q.empty()){v=q.front();q.pop();t[0]=v/1000;t[1]=v%1000/100;t[2]=v%100/10;t[3]=v%10;
// printf("%d %d %d %d",t[0],t[1],t[2],t[3]);for(j=0;j<4;j++){temp=t[j];for(i=0;i<10;i++)if(i!=temp){t[j]=i;vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];if(!dis[vtemp] && prime[vtemp]){count[vtemp]=count[v]+1;dis[vtemp]=true;q.push(vtemp);}if(vtemp==last) return count[vtemp];}t[j]=temp;}if(v==last) return count[v];}return -1;
}int main(){int n,a,b,key;init();scanf("%d",&n);while(n--){scanf("%d%d",&a,&b);key=bfs(a,b);if(key!=-1) printf("%d\n",key);else printf("Impossible\n");}return 0;
}
poj3126 Prime Path BFS相关推荐
- POJ3126 Prime Path(BFS)
题意: 从一个素数变到另一个素数,要求每次只能动一位上的数字,并且要求中间值也必须是素数 要点: 简单的BFS水题,只要将每个节点的变动方向写清楚就行了. 15306522 Seasonal 3126 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】
POJ3126Prime Path 给定两个四位素数a b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- Prime Path(bfs)广度优先搜索
题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(BFS + 素数打表)
题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数. 解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况 ...
- POJ 3126 - Prime Path + Python(BFS)
()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...
最新文章
- oracle vm virtualbox如何设置u盘启动_电脑自主U盘装机 如何设置U盘启动
- centos7安装java8
- 文献阅读 - Genome-wide consequences of deleting any single gene
- win2003下APACHE2.050+PHP5+MYSQL4.0.20+PHPMYADMIN2.57 的简易安装配置
- 月薪20k的web前端开发程序员,他们都会的这6招
- spring boot初体验(三)Spring AOP
- Leetcode 99. 恢复搜索二叉树
- ubuntu系统,网页版音乐播放器无声音
- 解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
- 【数据科学】肯德尔等级相关系数( Kendall's tau coefficient )
- 深圳中学因招聘上热搜:名校博士挤破头想进,教学成绩也确实不服不行
- 有没有集工作记录、项目时间线于一身的便签软件?
- vue3.2的TS写法
- 罗技g502鼠标宏设置教程分享
- 计算机内存如何查询,如何查看电脑物理内存
- asdfasdfsd阿萨德发撒的发撒的发撒的发
- top与free命令详解
- 基于艾宾浩斯遗忘曲线的APP(大学毕业设计)
- Linux 学习笔记3 -- 常用 Linux 命令的基本使用
- Laradock Mac下的环境配置和安装
热门文章
- [POJ2976] Dropping tests
- SetForceGroundWindow
- 我的世界1.7.10java下载安装_我的世界1.7.10正式版
- elementui 响应式导航栏网站_什么是响应式网站?响应式网站的优势介绍
- python3--htmltestrunner 日志显示_实时日志:Serverless Python 运行时支持日志实时输出...
- 翻译职称计算机能力,2018年职称计算机word2003考点辅导:用好Office2003中的翻译功能...
- Linux 进程通信fifo,Linux 进程通信之FIFO的实现
- 与数学表达式对应的python表达式_与数学表达式cd/2ab对应的Python表达式中,不正确的是:...
- 高端计算机教室,又一所高端学校来了,能住校师资力量强大
- html5离线保存需要联网吗,html5 离线存储