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.

  1. 1033
  2. 1733
  3. 3733
  4. 3739
  5. 3779
  6. 8779
  7. 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.

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

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

CN

Output - 输出

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

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

CN

Sample Input - 输入样例

  1. 3
  2. 1033 8179
  3. 1373 8017
  4. 1033 1033

Sample Output - 输出样例

  1. 6
  2. 7
  3. 0

题解

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

代码 C++

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #define mx 10000
  5. char prim[mx] = { }, data[];
  6. int len[mx];
  7. void rdy(){
  8. int i, j;
  9. for (i = ; i < mx; i += ) prim[i] = ;
  10. for (i = ; i < mx; i += ){
  11. if (prim[i]) continue;
  12. for (j = i << ; j < mx; j += i) prim[j] = ;
  13. }
  14. for (i = ; i < ; ++i) prim[i] = ;
  15. }
  16. int getData(){
  17. int i, opt = ;
  18. for (i = ; i < ; ++i) opt = opt * + data[i] - '';
  19. return opt;
  20. }
  21. void setData(int a){
  22. for (int i = ; ~i; --i) data[i] = '' + a % , a /= ;
  23. }
  24. int main(){
  25. rdy();
  26. int t,st, ed, now, nxt,i;
  27. char j, tmp;
  28. scanf("%d", &t);
  29. while (t--){
  30. scanf("%d%d", &st, &ed);
  31. memset(len, 0x7F, sizeof len); len[st] = ;
  32. std::queue<int> q; q.push(st);
  33. while (!q.empty()){
  34. setData(now = q.front()); q.pop();
  35. for (i = ; i < ; ++i){
  36. tmp = data[i];
  37. for (j = ''; j <= ''; ++j){
  38. if (j == tmp) continue;
  39. data[i] = j; nxt = getData();
  40. if (prim[nxt] || len[now] + >= len[nxt]) continue;
  41. len[nxt] = len[now] + ; q.push(nxt);
  42. }
  43. data[i] = tmp;
  44. }
  45. }
  46. printf("%d\n", len[ed]);
  47. }
  48. return ;
  49. }

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. 基础 MySQL

    .一.MySQL概述 1.什么是数据库  答:数据的仓库,如:在ATM的示例中我们创建了一个 DB 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL ...

  2. JSP FreeMarker Velocity 原理

    JSP原理 JSP的运行原理:JSP 本质上是一个Servlet. 每个JSP 页面在第一次被访问时,JSP引擎将它翻译成一个Servlet 程序,然后再把这个 Servlet 源程序编译成Servl ...

  3. 设计模式之Mediator(中介者)(转)

    Mediator定义: 用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator? 各个对象之间的交互操作非常多;每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉及到修改很 ...

  4. multiple definition of qt_plugin_query_metadata

    dustije 5 years ago I have a project with several plugins i want to compile into one library. I get ...

  5. 深度点评五种常见WiFi搭建方案

    总结十年无线搭建经验,针对企业常见的五种办公室无线网络方案做个简要分析,各种方案有何优劣,又适用于那种类型的企业. 方案一:仅路由器或AP覆盖 简述:使用路由器或AP覆盖多个无线盲区,多个AP的部署实 ...

  6. python之字符串函数

    1.  endswith()  startswith() # 以什么什么结尾 # 以什么什么开始 test = "alex" v = test.endswith('ex') v = ...

  7. 一个六年Java程序员的从业总结:比起掉发,我更怕掉队

    我一直担惊受怕,过去,可能是因为我年轻,但现在,我已经不是那么年轻了,我仍然发现有很多事情让我害怕. 当年纪越来越大后,我开始变得不能加班.我开始用更多的时间和家人在一起,而不是坐在计算机前(尽管这样 ...

  8. 怎样从外网访问内网Oracle数据库?

    本地安装了一个Oracle数据库,只能在局域网内访问到,怎样从外网也能访问到本地的Oracle数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Oracle数据库 默认安装的Or ...

  9. 【react懒加载组件】--react-lazyload

    组件安装: npm install react-lazyload --save-dev 组件使用: //引入 import LazyLoad from 'react-lazyload'; //rend ...

  10. nodejs安装淘宝npm镜像【cnpm】

    安装完nodejs后[自带npm] 如果npm无法使用或需要FQ,可以先安装cnpm,然后使用cnpm install安装模块 安装全局cnpm npm install -g cnpm --regis ...