POJ 3126 Prime Path(素数路径)

Time Limit: 1000MS    Memory Limit: 65536K

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.

内阁部长对安全部长发表改变办公室四位数房间号码的声明感到诚惶诚恐。
— 随时换号码是安全的做法,毕竟暗潮四伏。
— 然而我选1033做法房间号是有理由的。我是首相(Prime minister),你懂的。
— 我知道,所以你的新号码8179也是一个素数(prime )。你只要在你办公室门上的旧号码前贴四个新数字就好了。
— 没那么简单。如果我把第一个数改成8,那么号码将变成8033,这可不是素数。
— 我懂,你是总理,有素数强迫症。
— 是啊!所以我必须找个通过素数路径从1033到8179的方法,就是只改变其中一个数,并从一个素数到另一个素数。 与此同时,财政部长正暗中观察。
— 别搞没必要的开销!一个数字可是要一英镑。
— 嗯,这种情况下我需要一个计算最小成本的程序。你知道有群廉价的软件大神吗?
— 当然知道。你看,这里就有个算法比赛……帮助总理找出任意两个给定的四位素数间最便宜的素数路径!当然第一个数字不能为零。这是上述情况的解法。 这种解法的成本是6镑。注意,第2步中的数字1不能在最后一步重新使用 - 必须买个新的1。

CN

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).

第一行为一个正整数:测试用例的数量(最多100)。
随后每个测试用例,一行有两个以空格分隔的数。每个数皆为四位素数(无前导零)。

CN

Output - 输出

  One line for each case, either with a number stating the minimal cost or containing the word Impossible.

每个用例一行,一个表示最少花费的数或单词Impossible。

CN

Sample Input - 输入样例

3
1033 8179
1373 8017
1033 1033

Sample Output - 输出样例

6
7
0

题解

  一开始没判断是否可行还是A了,完全没有注意那个Impossible。
  最初设想用DFS,然而感觉没有BFS方便。
  打个素数表,做个标记,然后BFS就能出来了。

代码 C++

 #include <cstdio>
#include <cstring>
#include <queue>
#define mx 10000
char prim[mx] = { }, data[];
int len[mx];
void rdy(){
int i, j;
for (i = ; i < mx; i += ) prim[i] = ;
for (i = ; i < mx; i += ){
if (prim[i]) continue;
for (j = i << ; j < mx; j += i) prim[j] = ;
}
for (i = ; i < ; ++i) prim[i] = ;
}
int getData(){
int i, opt = ;
for (i = ; i < ; ++i) opt = opt * + data[i] - '';
return opt;
}
void setData(int a){
for (int i = ; ~i; --i) data[i] = '' + a % , a /= ;
}
int main(){
rdy();
int t,st, ed, now, nxt,i;
char j, tmp;
scanf("%d", &t);
while (t--){
scanf("%d%d", &st, &ed);
memset(len, 0x7F, sizeof len); len[st] = ;
std::queue<int> q; q.push(st);
while (!q.empty()){
setData(now = q.front()); q.pop();
for (i = ; i < ; ++i){
tmp = data[i];
for (j = ''; j <= ''; ++j){
if (j == tmp) continue;
data[i] = j; nxt = getData();
if (prim[nxt] || len[now] + >= len[nxt]) continue;
len[nxt] = len[now] + ; q.push(nxt);
}
data[i] = tmp;
}
}
printf("%d\n", len[ed]);
}
return ;
}

POJ 3126 Prime Path(素数路径)的更多相关文章

  1. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  2. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  3. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  4. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  5. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

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

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

  8. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  9. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

随机推荐

  1. Redis入门——安装与基本命令

    1. Redis安装 下载地址:https://github.com/MSOpenTech/redis/releases 下载zip文件后直接解压 2. 启动Redis服务端 解压目录下执行redis ...

  2. Vue:将px转化为rem,适配移动端vant-UI等框架(px2rem-loader)

    转载:https://www.cnblogs.com/WQLong/p/7798822.html 1.下载lib-flexible 使用的是vue-cli+webpack,通过npm来安装的 npm ...

  3. flask实战-个人博客-使用蓝本模块化程序

    使用蓝本模块化程序 实例化flask提供的blueprint类就创建一个蓝本实例.像程序实例一样,我们可以为蓝本实例注册路由.错误处理函数.上下文处理函数,请求处理函数,甚至是单独的静态文件文件夹和模 ...

  4. 【Alpha版本】冲刺阶段——Day6

    [Alpha版本]冲刺阶段--Day6 阅读目录 今日进展 问题困难 明日任务 今日贡献量 TODOlist [今日进展] 为注册模块增加界面代码 public static void Windows ...

  5. 世界最顶级邮件服务器组合Linux + PMTA + OEMPRO,PowerMTA 安装

    世界最顶级邮件服务器组合Linux + PMTA + OEMPRO PowerMTA 安装 PMTA + OEMPRO  这个是发送的组合 PMTA提供的SMTP,OEMPRO是订阅管理以及邮件的过滤 ...

  6. 计算概论(A)/基础编程练习2(8题)/6:数组逆序重放

    #include<stdio.h> int main() { // 输入n个整数 ; scanf("%d", &n); // 循环读入元素 while(scan ...

  7. 结合sklearn的可视化工具Yellowbrick:超参与行为的可视化带来更优秀的实现

    https://blog.csdn.net/qq_34739497/article/details/80508262 Yellowbrick 是一套名为「Visualizers」的视觉诊断工具,它扩展 ...

  8. linux 安装sqlite3

    python2个版本导致的问题. 网上找了好多方法都不行. 最后自己莫名其妙弄好了, 回想了一下大概是 安装sqlite3 重新安装python 最后 yum update 更新 就好了.

  9. centos6编译安装mysql5.5

    常规编译安装:./configure;make;make install centos 6.5,安装mysql 5.5.54,所需安装包cmake-2.8.8.tar.gz.mysql-5.5.54. ...

  10. P1290 欧几里德的游戏

    P1290 欧几里德的游戏 原本不想写的,但细节有些多qwq,还是放上吧. 假设a严格大于b 当a<b*2时,只有一种方法往下走:否则就可以有多种方法,并且一定至少有一种可以使自己必胜,因为可以 ...