kuangbin带你飞专题POJ - 3126 Prime Path
Posted 汤姆的猫生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kuangbin带你飞专题POJ - 3126 Prime Path相关的知识,希望对你有一定的参考价值。
原题重现:
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
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).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
大概题意:
给定两个四位数素数,起始素数为a, 终止素数为b, 求 从素数a 变成 素数b 的最短路径
条件:每次只能够变化其中一位数字, 且变化后的素数仍然为素数, 如 1033 到 3733 。
思路:
素数判定 + BFS。四位数中(1000到9999)中寻找到所有的素数,并存入数组。在在a开始,从素数组中搜索下一个素数(不同位数为1)。
本以为会超时,然鹅还是过了。
另外记录一个不太懂的地方,自己设了一个vis数组来标记,
为什么后面的memset初始化不管用呢(新手疯狂挠头,最后还是for重初始化)
代码实现
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std;
int total_size;
int vis[10005];
vector<int> prime;
struct node
int val;
int step;
node(int val, int step):val(val),step(step)
;
//素数判定
bool isPrime(int num)
if (num <= 3)
return num > 1;
if (num % 6 != 1 && num % 6 != 5)
return false;
int sqrt_n = (int)sqrt((double)num);
for (int i = 5; i <= sqrt_n; i += 6)
if (num % i == 0 || num % (i + 2) == 0)
return false;
return true;
//检查下一个是否符合要求
bool checkNext(int a, int b)
int flag = 0;
while(1)
int c = a % 10;
int d = b % 10;
a = a / 10;
b = b / 10;
if(c != d) flag++;
if(a == 0 || b == 0) break;
if(flag == 1) return true; //若只有一位不同,则可
return false; //否则,则不可
int main()
//素数组
for(int i = 1000; i < 9999; i++)
if(isPrime(i))
prime.push_back(i);
total_size = prime.size();
int T;
cin >> T;
while(T--)
int a, b, flag, step;
cin >> a >> b;
if(a == b)
cout << 0 << endl;
continue;
//这里不懂为什么用memset(vis, 0, sizeof(vis)) 毫无卵用呢。。
for(int i = 0; i < 10005; i++)
vis[i] = 0;
vis[a] = 1;
flag = 0;
queue<node> sol;
sol.push(node(a, 0));
//队列完成BFS操作
while(sol.size())
node ele = sol.front();
sol.pop();
for(int i = 0; i < total_size; i++)
if(vis[prime[i]] == 1) continue;
if(checkNext(prime[i], ele.val)) //检查下一个素数是否满足要求
if(prime[i] == b) //如果为结果就直接退出
flag = 1;
step = ele.step + 1;
break;
sol.push(node(prime[i], ele.step + 1)); //否则,进队列
vis[prime[i]] = 1; //这里标记已经进过队列了
if(flag == 1)
cout << step << endl;
break;
return 0;
以上是关于kuangbin带你飞专题POJ - 3126 Prime Path的主要内容,如果未能解决你的问题,请参考以下文章
Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
跟着chengyulala刷题之[kuangbin带你飞]之'并查集'专题/斜眼笑
Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索