题意为给出两个四位素数A、B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B。可以直接进行BFS搜索

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isPrime(int n){//素数判断
  5. if(n == || n == ) return true;
  6. else{
  7. int k = sqrt(n) + ;
  8. for(int i = ; i < k; i++){
  9. if(n % i == ) return false;
  10. }
  11. return true;
  12. }
  13. }
  14.  
  15. bool Prime[];
  16. int visit[];
  17. void getPrime(){
  18. for(int i = ; i < ; i++){
  19. if(isPrime(i))Prime[i] = true;
  20. }
  21. }
  22.  
  23. struct Node{
  24. int num;//储存数字
  25. int cost;//操作步数
  26. };
  27.  
  28. Node bfs(int start, int end){
  29. queue<Node> q;
  30. Node front;
  31. front.num = start;
  32. front.cost = ;
  33. visit[start] = ;
  34. q.push(front);
  35. while(!q.empty()){
  36. front = q.front(); q.pop();
  37.  
  38. for(int i = ; i < ; i++){//换千位
  39. int m = front.num;
  40. m = m % + i * ;
  41. if(!visit[m] && Prime[m]){
  42. visit[m] = ;
  43. Node tmp = front;
  44. tmp.num = m;
  45. tmp.cost++;
  46. q.push(tmp);
  47.  
  48. if(m == end)return tmp;
  49. }
  50. }
  51.  
  52. for(int i = ; i < ; i++){//换百位
  53. int m = front.num;
  54. m = m % + (m/) * + i * ;
  55. if(!visit[m] && Prime[m]){
  56. visit[m] = ;
  57. Node tmp = front;
  58. tmp.num = m;
  59. tmp.cost++;
  60. q.push(tmp);
  61.  
  62. if(m == end)return tmp;
  63. }
  64. }
  65.  
  66. for(int i = ; i < ; i++){//换十位
  67. int m = front.num;
  68. m = m % + (m/) * + i * ;
  69. if(!visit[m] && Prime[m]){
  70. visit[m] = ;
  71. Node tmp = front;
  72. tmp.num = m;
  73. tmp.cost++;
  74. q.push(tmp);
  75. if(m == end)return tmp;
  76. }
  77. }
  78.  
  79. for(int i = ; i < ; i++){//换个位
  80. int m = front.num;
  81. m = (m/) * + i;
  82. if(!visit[m] && Prime[m]){
  83. visit[m] = ;
  84. Node tmp = front;
  85. tmp.num = m;
  86. tmp.cost++;
  87. q.push(tmp);
  88.  
  89. if(m == end)return tmp;
  90. }
  91. }
  92. }
  93. Node tmp;
  94. tmp.num = ;
  95. tmp.cost = ;
  96. return tmp;
  97. }
  98. int main(){
  99. int n;
  100. cin >> n;
  101. getPrime();
  102. while(n--){
  103. int a, b;
  104. cin >> a >> b;
  105. Node tmp;
  106. memset(visit, , sizeof(visit));
  107. tmp = bfs(a, b);
  108. if(tmp.num == && tmp.cost == ) cout << << endl;
  109. else{
  110. cout << tmp.cost << endl;
  111. }
  112. }
  113. }

Sicily 1444: Prime Path(BFS)的更多相关文章

  1. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  3. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  4. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  5. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  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)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27852   Accepted: 15204 Desc ...

随机推荐

  1. Basic EEG waves 四种常见EEG波形

    Source: https://www.medicine.mcgill.ca/physio/vlab/biomed_signals/eeg_n.htm The electroencephalogram ...

  2. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. WPF简单模拟QQ登录背景动画

    介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...

  5. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  6. Mybatis关联查询和数据库不一致问题分析与解决

    Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...

  7. 理解OAuth 2.0

    转自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...

  8. [转]HQL中的子查询

    原文地址:http://blog.csdn.net/xb12369/article/details/8638683 子查询:   子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一 ...

  9. Markdown

    1. 斜体和粗体 代码: *斜体*或_斜体_ **粗体** ***加粗斜体*** ~~删除线~~ 显示效果: 这是一段斜体 这是一段粗体 这是一段加粗斜体 这是一段删除线 2. 分级标题 第一种写法: ...

  10. PHP 做文件校验,MD5,CRC32,SHA等

    函数 hash_file(): 使用给定文件的内容生成哈希值 说明 string hash_file ( string $algo , string $filename [, bool $raw_ou ...