EOJ1646 bfs

Prime Path

Time Limit:1000MSMemory Limit:65536KB
Total Submit:68Accepted:40

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

方品   10121540117

题目:EOJ1646

 

题目分析:

最短路径问题,想到用队列实现bfs求解。可构造一个结构体存储四位数的每一位。先求出四位数的质数,这样可在o(1)时间内判断一个数是否为素数。将第一个数放入队列,每次取出队首元素,依次改变其各位,若为质数,则进队。注意这里可以剪枝,若新的质数在之前已经遍历过了则不再进队。可通过一个hash数组标记该数是否被访问过,若对手元素为所求元素则返回路径长度,若队空跳出循环则说明不存在答案。

 

AC代码:

#include

#include

#include

#include

#include

#include

#include

#include

 

using namespace std;

 

bool prime[10005];

bool used[10005];

 

struct Num{

   int a[4],step;

   Num(){memset(a,0,sizeof(a));step=0;}

   int val(){

       return a[0]*1000+a[1]*100+a[2]*10+a[3];

    }

};

void setprime(){

   int i,j;

   for(i=0;i<10005;++i) prime[i]=i;

   i=2;

   while(i<101){

       if(prime[i])

           for(j=2;i*j<10005;++j)

                prime[i*j]=false;

       ++i;

    }

}

 

int bfs(string a,string b){

   queue q;

   Num now,cmp;

   for(int i=0;i<4;++i){

       now.a[i]=a[i]-'0';

       cmp.a[i]=b[i]-'0';

    }

   q.push(now);

   used[now.val()]=true;

   while(!q.empty()){

       now=q.front();

       q.pop();

       for(int i=0;i<4;++i){

           Num next=now;

           for(int j=0;j<=9;++j){

                if(i==0 && j==0)continue;

                next.a[i]=j;

                next.step=now.step+1;

                if(next.val()==cmp.val()){

                    return next.step;

                }

                if( !used[next.val()]&& prime[next.val()] ){

                    q.push(next);

                    used[next.val()]=true;

                }

           }

        }

    }

   return -1;

}

 

int main()

{

   int t;

   setprime();

   cin>>t;

   while(t--){

       memset(used,false,sizeof(used));

       string a,b;

       cin>>a>>b;

       if(a==b)

           cout<<"0"<

       else{

           int ans=bfs(a,b);

           if(ans!=-1)

                cout<

           else

               cout<<"Impossible"<

       }

    }

   return 0;

}



你可能感兴趣的:(EOJ,bfs,bfs)