HDU1973 POJ3126 UVA12101 LA3639 Prime Path【BFS】

Prime Path

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1388 Accepted Submission(s): 866

Problem 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

Source
NWERC2006

问题链接HDU1973 POJ3126 UVA12101 LA3639 Prime Path
问题简述:(略)
问题分析
    求素数最短路径问题,每个结点是四位数的素数,结点间一位不同则有边相连。最短路径问题通常用BFS来解决。
    预先用筛选法求得所有四位数的素数备用,可以加速。出现过的四位数素数做个标记,不用重复搜索。
程序说明
    这个解题程序代码可以改进,可以用2重循环来替代,可以简化代码。
    这个程序在UVALive中提交出现WA。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU1973 POJ3126 UVA12101 LA3639 Prime Path */

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int N = 10000;
const int SQRTN = sqrt((double) N);
bool isprime[N + 1];
// Eratosthenes筛选法
void esieve(void)
{
    memset(isprime, true, sizeof(isprime));

    isprime[0] = isprime[1] = false;
    for(int i = 2; i <= SQRTN; i++) {
        if(isprime[i]) {
            for(int j = i * i; j <= N; j += i)  //筛选
                isprime[j] = false;
        }
    }
 }

int vis[N];
int cnt, sp, ep;

void bfs(int p)
{
    memset(vis, 0, sizeof(vis));

    queue<pair<int, int> > q;

    q.push(make_pair(0, p));
    while(!q.empty()) {
        pair<int, int> t = q.front(), n;
        q.pop();
        if(t.second == ep) {
            cnt = t.first;
            return;
        }

        // 个位,只能是奇数
        int d1 = t.second % 10;
        for(int i = 1; i < 10; i += 2) {
            if(i != d1) {
                n = t;
                n.second -= d1;
                n.second += i;
                if(isprime[n.second] && vis[n.second] == 0) {
                    vis[n.second] = 1;
                    n.first++;
                    q.push(n);
                }
            }
        }

        // 十位
        d1 = t.second / 10 % 10;
        for(int i = 0; i < 10; i++) {
            if(i != d1) {
                n = t;
                n.second -= d1 * 10;
                n.second += i * 10;
                if(isprime[n.second] && vis[n.second] == 0) {
                    vis[n.second] = 1;
                    n.first++;
                    q.push(n);
                }
            }
        }

        // 百位
        d1 = t.second / 100 % 10;
        for(int i = 0; i < 10; i++) {
            if(i != d1) {
                n = t;
                n.second -= d1 * 100;
                n.second += i * 100;
                if(isprime[n.second] && vis[n.second] == 0) {
                    vis[n.second] = 1;
                    n.first++;
                    q.push(n);
                }
            }
        }

        // 千位,不能是0
        d1 = t.second / 1000;
        for(int i = 1; i < 10; i++) {
            if(i != d1) {
                n = t;
                n.second -= d1 * 1000;
                n.second += i * 1000;
                if(isprime[n.second] && vis[n.second] == 0) {
                    vis[n.second] = 1;
                    n.first++;
                    q.push(n);
                }
            }
        }
    }
}

int main()
{
    esieve();

    int t;
    scanf("%d\n", &t);
    while(t--) {
        scanf("%d%d", &sp, &ep);

        cnt = -1;
        bfs(sp);

        if(cnt == -1) printf("Impossible\n");
        else printf("%d\n", cnt);
    }

    return 0;
}
发布了2126 篇原创文章 · 获赞 2306 · 访问量 254万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/104477380