POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 22936 | Accepted: 12706 |
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.
Input
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Source
题解:
1.打印素数表。
2.由于只有四位数,直接枚举不会超时。由于要求的是“最少步数”,所以用BFS进行搜索。
写法一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node //num为素数,dig[]为这个素数的每个位上的数,便于操作。
{
int num, dig[], step;
}; int vis[], pri[]; queue<node>que;
int bfs(node s, node e)
{
ms(vis,);
while(!que.empty()) que.pop();
s.step = ;
vis[s.num] = ;
que.push(s); node now, tmp;
while(!que.empty())
{
now = que.front();
que.pop(); if(now.num==e.num)
return now.step; for(int i = ; i<; i++) //枚举位数
for(int j = ; j<; j++) //枚举数字
{
if(i== && j==) continue; //首位不能为0
tmp = now;
tmp.dig[i] = j; //第i为变为j
tmp.num = tmp.dig[] + tmp.dig[]*+tmp.dig[]*+tmp.dig[]*;
if(!pri[tmp.num] && !vis[tmp.num]) //num为素数并且没有被访问
{
vis[tmp.num] = ;
tmp.step++;
que.push(tmp);
}
}
}
return -;
} void init() //素数表,pri[]==0的为素数
{
int m = sqrt(+0.5);
ms(pri,);
pri[] = ;
for(int i = ; i<=m; i++) if(!pri[i])
for(int j = i*i; j<=; j += i)
pri[j] = ;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n, m;
node s, e;
scanf("%d%d",&n,&m);
s.num = n; e.num = m;
for(int i = ; i<; i++, n /= ) s.dig[i] = n%;
for(int i = ; i<; i++, m /= ) e.dig[i] = m%; int ans = bfs(s,e);
if(ans==-)
puts("Impossible");
else
printf("%d\n",ans);
}
}
写法二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int num, step;
}; int vis[], dig[];
int pri[]; queue<node>que;
int bfs(int s, int e)
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.num = s;
now.step = ;
vis[now.num] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.num==e)
return now.step; dig[] = now.num%;
dig[] = (now.num/)%;
dig[] = (now.num/)%;
dig[] = (now.num/)%;
for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
if(i== && j==) continue;
tmp.num = now.num + (j- dig[i])*pow(,i); //pow前面不要加上强制类型(int)
// tmp.num = now.num + (j- dig[i])* (int)pow(10,i);
// (int)pow(10,2) 居然等于99, 看来还是不要依赖这些函数
if(!pri[tmp.num] && !vis[tmp.num])
{
vis[tmp.num] = ;
tmp.step = now.step + ;
que.push(tmp);
}
}
}
return -;
} void init()
{
int m = sqrt(+0.5);
ms(pri,);
pri[] = ;
for(int i = ; i<=m; i++) if(!pri[i])
for(int j = i*i; j<=; j += i)
pri[j] = ;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n, m;
scanf("%d%d",&n,&m);
int ans = bfs(n,m);
if(ans==-)
puts("Impossible");
else
printf("%d\n",ans);
}
}
POJ3126 Prime Path —— BFS + 素数表的更多相关文章
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- [POJ]P3126 Prime Path[BFS]
[POJ]P3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35230 Accepted: ...
随机推荐
- [配置Cordova环境] [Alfred使用手册]
Mac神器 Alfred使用手册http://www.tuicool.com/articles/YJJv2i 配置Cordova环境 1.到nodejs官网下载最新版本,安装pkg文件 2.终端运行 ...
- 洛谷 [P1290] 欧几里得的游戏
SG函数的应用 看到这题就想到了SG函数 那么可以考虑最终情况:一个数是x,另一个是0,那么先手必败(因为上一个人已经得到0了,其实游戏已经结束了) 剩下的情况:一个数n, 一个数m,假设n>m ...
- springboot开发 第一个案例之hello,world!
开发环境:(1)Eclipse Java EE Version: Neon Release (4.6.0) Build id: 20160613-1800 (2)apache-maven-3 ...
- Linux下AT&T汇编语法格式与Intel汇编语法格式异同
由于绝大多数的国内程序员以前只接触过Intel格式的汇编语言,很少或几乎没有接触过AT&T汇编语言,虽然这些汇编代码都是Intel风格的.但在Unix和Linux系统中,更多采用的还是AT&a ...
- Redis的内部运作机制
本文将分五个部分来分析和总结Redis的内部机制,分别是:Redis数据库.Redis客户端.Redis事件.Redis服务器的初始化步骤.Redis命令的执行过程. 首先介绍一下Redis服务器的状 ...
- android本地存储SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- golang sort包 排序
[]float64: ls := sort.Float64Slice{ 1.1, 4.4, 5.5, 3.3, 2.2, } fmt.Println(ls) //[1.1 4.4 5.5 3.3 2. ...
- pnputil
http://mb.yidianzixun.com/article/0FYSZgnB?s=mb&appid=mibrowser C:\Users\Administrator>pnputi ...
- JS--截取字符串常用方法详细
使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=the ...
- [转]java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...