POJ 3126 Prime Path(BFS + 素数打表)
题意:给定两个四位素数, 从一个素数到另一个素数,最少用几步,可以一次更改四位中的任意一位,但每次改变都只能是素数。
解题思路:四位数每一位情况有十种情况0-9, 四位共有40种情况, 枚举40种情况,拿出来判断是不是素数表中的,如果是就压入队列,不是就抛弃。
Description
— 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
Output
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>using namespace std;
typedef pair<int, int> P;
#define MAX_N 10000
const int INF = 0x3f3f3f3f;
int n , m;
bool prime[MAX_N];
bool vis[MAX_N];
int d[MAX_N];//void make_prime()
//{
// memset(prime, true, sizeof(prime));
// for(int i = 1000 ; i <= MAX_N ; i++)
// {
// for(int j = 2 ; j < i ; j++)
// {
// if(i % j == 0)
// {
// prime[i] = false;
// break;
// }
// }
// }for(int i = 1000 ; i <= MAX_N ; i++)if(prime[i] == true) cout<<i<<endl;
//}void init() //对素数打表
{int i,j;for(i=1000; i<MAX_N; i++){for(j=2; j<i; j++)if(i%j==0){prime[i]=false;break;}if(j==i) prime[i]=true;}
}int bfs(int n ,int m)
{memset(vis, false, sizeof(vis));memset(d, -1, sizeof(d));queue<int> que;vis[n] = true;d[n] = 0;que.push(n);while(!que.empty()){int p = que.front();que.pop();if(p == m)return d[p];int t[4];t[0] = p / 1000;t[1] = p % 1000 / 100;t[2] = p % 100 / 10;t[3] = p % 10;for(int i = 0 ; i < 4 ; i++){int tmp = t[i];for(int j = 0 ; j < 10 ; j++){if(j != tmp){t[i] = j;int num = t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];if(num >= 1000 && !vis[num] && prime[num]){
// cout<<num<<endl;d[num] = d[p] + 1;vis[num] = true;que.push(num);}}}t[i] = tmp;}}return -1;
}int main()
{init();
// freopen("2.txt","w",stdout);
// for(int i = 0 ; i < MAX_N ; i++)
// if(prime[i])
// cout<<i<<endl;int t;scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);int ans = bfs(n, m);if(ans == -1)cout<<"Impossible"<<endl;elsecout<<ans<<endl;}return 0;
}
POJ 3126 Prime Path(BFS + 素数打表)相关推荐
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 - Prime Path + Python(BFS)
()原题链接: # 原题:POJ 3126 - Prime Path | 眈眈探求 # 解题思路:https://blog.csdn.net/LYHVOYAGE/article/details/182 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- poj 3126 Prime Path
看题目... 看解题报告... 敲.... 题意转自:優YoU http://user.qzone.qq.com/289065406/blog/1303623014 大致题意: 给定两个四位素数a ...
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
- POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...
- POJ 3126 Prime Path
水题:直接判断素数+bfs 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #in ...
- poj3126 Prime Path BFS
点击打开链接 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26158 Accepted: 143 ...
最新文章
- java冒泡函数解释,JS DOM操作 函数 事件 阻止事件冒泡
- crypto——明文攻击
- 文件操作:fread()和fwrite()
- Mysql数据库的基本概念
- Java——List集合特有的功能
- sys.argv和getopt
- 怎样写一篇优秀论文?看完受益匪浅!
- 基础层区块链Harmony发布主网新版本v4.0.0
- Overview of HEVC之4 帧内预测
- java 复杂表达式计算_我的计算器. 用java实现的. 可以支持复杂表达式
- 驱动开发——经典图书免费试读下载及勘误讨论
- Java实现自定义工作流
- 用Python实现一个电影订票系统
- @submit.native.prevent
- 小米盒子显示连不上服务器,小米盒子连不上WiFi?这七个技巧绝对管用
- 图片放大-jQuery脚本使用异常:仅闪现图片外框
- 实践课题选题管理(C语言)(选课系统)
- Conky-colors详细教程
- Ubuntu 20.04.2.0 LTS 下Geany 1.36的“编译文件”和“生成当前文件”两个按键不可用
- 多发性硬化功能磁共振成像
热门文章
- BZOJ 3168: [Heoi2013]钙铁锌硒维生素
- h5页面中android与ios返回上一级并强制刷新的方式
- 汇金蛛:推荐在家0成本,有营养的副业兼职
- UI设计书籍推荐,这三本好书你不能错过
- Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
- 服务器光猫一直亮不响应,光猫光信号正常,且光猫和路由器都重启过,还是上不了网咋办?...
- 信息学奥赛一本通c++【1033】
- android系统自带system/app下载,Android 把应用APK安装包放到system/app下的方法
- 64位操作系统注册ocx控件失败,提示:模块加载失败请确保该二进制存储在指定路径中。
- element中滑块组件Slider展示一天24小时的时间问题