首页 > 代码库 > POj 3126 Prime Path

POj 3126 Prime Path

来源:http://poj.org/problem?id=3126


Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11384 Accepted: 6453

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

Northwestern Europe 2006

题目大意:  从素数四位数a变换到四位数b,每次只能改变一位数字,且每次改变后的数也必须为素数~~ 四位数中第一位不能为0,求最少变换次数,如果无法变换,则输出"Impossible".

题解:明显的广搜~~ 每次改变一位数,用hash判重解决之。

PS:这题出现了一个很蛋疼的错误,昨天样例都可以过, 但是提交WA。今天重写一遍-----发现把++exdir 写成exidir++囧..........

 AC代码:

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int Max=2500000,Maxh=10005;
bool visit[Maxh]={0},Isprime[Maxh]={0};
int  star,dest;
bool prime(int k){
    for(int i=2;i<=(int)sqrt((float)k);i++)
    if(k%i==0)
    return false;
    return true;
}
struct Node{
    int dig[4],num,step;
}map[Max]={0},temp;
void calc(Node &x){
x.dig[0]=x.num/1000;x.dig[1]=(x.num/100)%10;
x.dig[2]=(x.num/10)%10;x.dig[3]=x.num%10;
}
void calcsum(Node &x){
    x.num=0;
    for(int i=0;i<4;i++)
    x.num+=x.dig[i]*(int)pow((float)10,3-i);
}
int main()
{
    int t;
    cin>>t;
    for(int i=1000;i<10000;i++)
    if(prime(i))
    Isprime[i]=1;
    while(t--){
    memset(visit,0,sizeof(visit));
    int flag=1;
    cin>>star>>dest;
    if(star==dest){
    cout<<0<<endl;
    continue;
    }
    map[0].num=star;  calc(map[0]);  map[0].step=0;
    int nodedir=0,exdir=0;
    while(nodedir<=exdir&&exdir<Max&&flag){
    for(int i=0;i<4&&flag;i++){
    for(int k=0;k<10;k++){
    if(i||k){
    if(i==3&&k%2==0)
    continue;
    temp=map[nodedir];
    temp.dig[i]=k;
    calcsum(temp);
    temp.step++;
    if(temp.num==dest){
    cout<<temp.step<<endl;
    flag=0;
    break;
    }
    if(Isprime[temp.num]&&!visit[temp.num]){
    visit[temp.num]=1;
    map[++exdir]=temp;
    }
    }
    }
    }
    nodedir++;
    }
    if(flag)
    cout<<"Impossible"<<endl;
    }
return 0;
}