Kattis之旅——Prime Path
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 1 | Sample Output 1 |
---|---|
|
|
大致意思就是由前面的那个素数变到后面的那个素数,每次只能变一位数,变化后的数也应该是一个素数(无论是不是所求的数),求变化次数。
直接BFS即可。
- //Asimple
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = ;
- ll n, m, s, res, ans, len, T, k;
- int x, y;
- int pr[maxn];
- int P(int n) {
- for(int i=; i*i<=n; i++) {
- if( n%i== ) return ;
- }
- return ;
- }
- //将k位化0
- int change(int n, int k) {
- char s[] = {};
- sprintf(s, "%d", n);
- s[k] = '';
- sscanf(s, "%d", &n);
- return n;
- }
- int solve(int s, int e) {
- queue<int> q;
- int dis[maxn] = {};
- q.push(s);
- dis[s] = ;
- while( q.size() ) {
- s = q.front();
- q.pop();
- if( s == e ) return dis[s]-;
- int t = ;
- for(int i=; i<; i++) {
- int k = change(s, i);
- for(int j=; j<; j++) {
- int a = k+j*t;
- if( pr[a]== && dis[a]== ) {
- q.push(a);
- dis[a] = dis[s]+;
- }
- }
- t /= ;
- }
- }
- return -;
- }
- void input() {
- for(int i=; i<maxn; i++) pr[i] = P(i);
- cin >> T;
- while( T -- ) {
- cin >> x >> y;
- ans = solve(x, y);
- if( ans==- ) puts("Impossible");
- else cout << ans << endl;
- }
- }
- int main(){
- input();
- return ;
- }
Kattis之旅——Prime Path的更多相关文章
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
随机推荐
- cordava打包vue项目成app
注意:安装目录不要以中文命名 1.安装cordova :npm install -g cordova 2.安装java jdk :配置环境变量: 1.系统变量:名:JAVA_HOME 值:C:\ ...
- [js]纯css强制不换行
要加在li上 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘
什么是以太坊(Ethereum) 以太坊(Ethereum)是一个基于区块链技术,允许任何人构建和使用去中心化应用的区块链平台.像比特币一样,以太坊是开源的,并由来自全世界的支持者们共同维护.与比特币 ...
- 【UML】-NO.42.EBook.5.UML.1.002-【UML 大战需求分析】- 活动图 (Activity Diagram)
1.0.0 Summary Tittle:[UML]-NO.42.EBook.1.UML.1.002-[UML 大战需求分析]- 活动图 Style:DesignPattern Series:Desi ...
- FreeMaker常用表达式
1,日期转换 ${data.startDate?string("yyyy-MM-dd HH:mm")} 2,非空检查 当数据为null时,1中日期转换在界面上不会显示异常,但在日志 ...
- zabbix 自定义监控文本内容
需求:监控服务器硬盘使用率是否有超过80%的 需要监控的文本 root@zabbix zabbix]# cat /etc/zabbix/scripts/data/monitor_disk.txt &q ...
- 【Redis】主从同步
Redis提供了主从复制功能,主要是为了保证服务的高可用性.在redis.conf配置文件中通过设置,可以开启主从复制功能.或者在客户端中使用slaveof 命令开启该功能. slaveof < ...
- 从零开始一起学习SLAM | 三维空间刚体的旋转
刚体,顾名思义,是指本身不会在运动过程中产生形变的物体,如相机的运动就是刚体运动,运动过程中同一个向量的长度和夹角都不会发生变化.刚体变换也称为欧式变换. 视觉SLAM中使用的相机就是典型的刚体,相机 ...
- 利用css伪类编写冒泡小三角
HTML代码 <div class="lf otherLogin"> <span>其他方式注册</span> <div class=&qu ...
- oracle中实现自增id
在一些数据库(例如mysql)中,实现自增id只要在建表的时候指定一下即可, 但是在oracle中要借助sequence来实现自增id, 要用上自增id,有几种方式: 1.直接在insert语句中使用 ...