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 |
|---|---|
3 |
6 |
大致意思就是由前面的那个素数变到后面的那个素数,每次只能变一位数,变化后的数也应该是一个素数(无论是不是所求的数),求变化次数。
直接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 ...
随机推荐
- Java字符串复制
Java字符串复制 public boolean topicFilterMatch(String topicFilter, String topic) { if (topicFilter == nul ...
- 使用web3j工具生成java版本的智能合约
这里需要使用的环境 web3j,nodejs 安装编译sol工具 $ npm install -g solc 保存为hello.sol文件到本地 pragma solidity 0.4.19; con ...
- 【Redis】主从同步
Redis提供了主从复制功能,主要是为了保证服务的高可用性.在redis.conf配置文件中通过设置,可以开启主从复制功能.或者在客户端中使用slaveof 命令开启该功能. slaveof < ...
- 遇到问题---hosts不起作用问题的解决方法
c:\WINDOWS\system32\drivers\etc\hosts 文件的作用是添加 域名解析 定向 比如添加 127.0.0.1 www.baidu.com 那我们访问www.baidu. ...
- cocos2dx 游戏plist与png完美切成小图python代码
首先需要一份python的切图程序: #python2.5 unpack_plist.py birdfly #! /usr/lical/bin/python import os,sys from xm ...
- cocos2d-x C++ (iOS)集成第三方微信分享
1.新建项目并下载 ShareSDK 1.Cocos2d-x项目环境搭建,不会的童鞋自行面壁哈: 网页链接. 2.ShareSDK iOS版本的 Cocos2d-x 插件是在ShareSDK iOS版 ...
- Pycharm进行版本管理
即然pycharm为python提供了这么强大的IDE,那么,我们代码管理,没理由不用版本管理工具Git,SVN等等 在pychram中使用GitHub进行代码管理;需要准备: 1)GitHub帐号: ...
- unity3d-多媒体与网络
1.音乐 unity3d 共支持4种音乐的格式文件 aiff:适用于较短的音乐文件,可用于游戏音效 wav:适用于较短的音乐文件,可用于游戏音效 mp3:适用于较长的音乐文件,可用于游戏音乐 ogg: ...
- spring之文件上传
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- sqlmap常用技巧整理
言 通过在乌云网上出现的很多SQL注入漏洞,因此来总结一下,大致使用SQLMAP所遇到的参数. 基本结构 基本SQLMAP的使用方式就如下所示,使用参数式的方式,按需求添加. 12 sqlmap.py ...