poj3126(Prime Path)广搜+素数判定

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

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

大水题,40入口的BFS,剪枝后远远没有40入口。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

const int maxp = 10010;

bool isPrime[maxp];
int a, b;

void init()
{
    fill(isPrime, isPrime+maxp, true);
    for (int i = 2; i < maxp; ++i)
        if (isPrime[i])
            for (int j = i*i; j < maxp; j += i)
                isPrime[j] = false;
}

int bfs()
{
    int vis[maxp];
    memset(vis, -1, sizeof(vis));
    queue<int> q;
    q.push(a);
    vis[a] = 0;
    while (!q.empty())
    {
        int num = q.front();
        q.pop();
        if (num == b)
            return vis[num];
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 10; ++j)
                if (i != 3 || j)
                {
                    int t = (int)(pow(10, i) + 0.5);
                    int c = num - ((num / t) % 10) * t + j * t;
                    if (isPrime[c] && vis[c] == -1)
                    {
                        q.push(c);
                        vis[c] = vis[num] + 1;
                    }
                }
    }
    return -1;
}

int main()
{
    init();
    int T;
    cin >> T;
    while (T--)
    {
        scanf("%d%d", &a, &b);
        int ans = bfs();
        if (ans == -1)
            printf("Impossible\n");
        else
            printf("%d\n", bfs());
    }
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-12-11 04:57:19

poj3126(Prime Path)广搜+素数判定的相关文章

POJ 3126 Prime Path( 广搜 )

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12974   Accepted: 7342 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-dig

POJ3126 Prime Path 打表+BFS

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

poj3126——Prime Path(BFS)

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

poj3126--Prime Path(广搜)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11751   Accepted: 6673 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-dig

POJ3126 Prime Path

http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 1033173337333739377987798179 分析:用字符串存m,n,这样改变各个位较方便 数组开大一点,开始数组开小了,结果就出错了 #include<stdio.h> #include<stdlib.h> #include<math.h> #include<qu

UVA 10200 Prime Time(简单素数判定预处理)

Euler is a well-known matematician, and, among many other things, he discovered that the formula n 2 + n + 41 produces a prime for 0 ≤ n < 40. For n = 40, the formula produces 1681, which is 41 ∗ 41. Even though this formula doesn’t always produce a

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

双向广搜 POJ 3126 Prime Path

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 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 th

poj 3126 prime path 简单广搜

http://poj.org/problem?id=3126 题意:给你两个四位数a,b,从a开始    每次只能改变上一次数的其中一位,问至少需要几步才能得到b 分析:求最小路   典型的广搜   表面上是    40入口的bfs   但是除去有的数不是素数   入口数远小于40 可以写一个  判断一个数是否为素数的函数  , 每次去 调用 判断一个数是否要进队列 也可以 事先打一个素数表  这样会快点 注意:output  :either with a number stating the