首页 > ACM题库 > HDU-杭电 > hdu 1973 Prime Path-最小生成树-[解题报告]C++
2013
12-26

hdu 1973 Prime Path-最小生成树-[解题报告]C++

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.

输入:

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).

输出:

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).

样例输入:

3
1033 8179
1373 8017
1033 1033

样例输出:

6
7
0

/* 
 * File:   hdu1973.cpp
 * Author: ssslpk
 * Created on August 28, 2012, 4:34 PM
 * 题意:给出两个四位数,现要改变第一个数中的个,十,百,千位当中的一个数
 * 使它最终变成第二个数,要求这过程中形成的数是素数,问最少的步骤
 * 
 * 题解:素数筛选+bfs
 */
#include <cstdlib>
#include<iostream>
#include<string.h>
#include<cstdio>
#include<math.h>
#include<queue>
#define maxn 10002


using namespace std;
int ispri[maxn];
int pri[maxn];
int plen;

void prime()
{
    memset(ispri,1,sizeof(ispri));
    plen=0;
    ispri[1]=ispri[0]=0;
    for(int i=2;i<maxn;i++)
    {
        if(ispri[i])
        {
            pri[plen++]=i;
            for(int j=2*i;j<maxn;j+=i)
                ispri[j]=0;
        }
    }
}
struct node
{
    int num;
    int id;
    int cnt;
};
int serch(int num,int id)
{
    while(id){num/=10;id--;}
    return num%10;
}
queue<node> Q;
int bfs(int s,int e)
{
    if(s==e)return 0;
    node first;
    first.num=s;
    first.id=-1;
    first.cnt=0;
    int vis[maxn];
    memset(vis,0,sizeof(vis));
    Q.push(first);
    vis[s]=1;
    while(!Q.empty())
    {
        node temp=Q.front();
        Q.pop();
        if(temp.num==e)
        {
            while(!Q.empty())Q.pop();
            return temp.cnt;
        }
        for(int j=0;j<4;j++)
        if(temp.id!=j)
        {
            int dig=serch(temp.num,j);
            for(int i=0;i<10;i++)
            if((j != 3 || i != 0)&& dig !=i)
            {
                node p;
                p.num=temp.num -(dig - i)*pow(10,j);
                p.id=j;
                p.cnt=temp.cnt+1;
                if(ispri[p.num]&& !vis[p.num])
                {
                    vis[p.num]=1;
                    Q.push(p);

                }
            }
        }
    }
    
}

int main(int argc, char** argv) {
    prime();
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int s,e;
        scanf("%d%d",&s,&e);
        printf("%d\n",bfs(s,e));
    }
    return 0;
}

解题转自:http://blog.csdn.net/ssslpk/article/details/7917032


  1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。