题目:
| Prime Path| Time Limit: 1000MS | | Memory Limit: 65536K |
| ---------------------------- | - | ------------------------ |
| Total Submissions: 18523 | | Accepted: 10409 |DescriptionThe 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 8179The 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.InputOne 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).OutputOne 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 1033Sample Output```
6 7 0
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - | - |
\[[Submit](http://poj.org/submit?problem_id=3126)] \[Go Back] \[[Status](http://poj.org/problemstatus?problem_id=3126)] \[[Discuss](http://poj.org/bbs?problem_id=3126)]
思路:
给了一个起始的数和结尾的数(都是素数),每次可以改变一个数字为素数(只有这个数字为素数,才可以改变这个数字为题目要求的数字),总共四种情况,个位,十位,百位,千位。分别枚举,剔除其中的非素数,然后进行广搜
代码:
```cpp
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int sushu[10005];
struct node
{
int num;
int step;
};
void biao()
{
mem(sushu,1);
sushu[1]=0;
for(int i=2; i<sqrt(10005); i++)
if(sushu[i])
for(int j=2; i*j<=10005; j++)
sushu[i*j]=0;
}
void bfs(int start,int stop)
{
node now,to;
queue<node>q;
now.num=start;
now.step=0;
q.push(now);
sushu[now.num]=0;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.num==stop)
{
printf("%d\n",now.step);
return;
}
int n1=now.num%10;//个位
int n2=now.num%100/10;//十位
int n3=now.num%1000/100;//百位
int n4=now.num/1000;//千位
for(int i=0; i<=9; i++) //枚举个位
{
to.num=n4*1000+n3*100+n2*10+i;
if(to.num!=now.num&&sushu[to.num])
{
to.step=now.step;
to.step++;
sushu[to.num]=0;
if(to.num==stop)
{
printf("%d\n",to.step);
return;
}
q.push(to);
}
}
for(int i=0; i<=9; i++) //枚举十位
{
to.num=n4*1000+n3*100+i*10+n1;
if(to.num!=now.num&&sushu[to.num])
{
to.step=now.step;
to.step++;
sushu[to.num]=0;
if(to.num==stop)
{
printf("%d\n",to.step);
return;
}
q.push(to);
}
}
for(int i=0; i<=9; i++) //枚举百位
{
to.num=n4*1000+i*100+n2*10+n1;
if(to.num!=now.num&&sushu[to.num])
{
to.step=now.step;
to.step++;
sushu[to.num]=0;
if(to.num==stop)
{
printf("%d\n",to.step);
return;
}
q.push(to);
}
}
for(int i=1; i<=9; i++) //枚举千位
{
to.num=i*1000+n3*100+n2*10+n1;
if(to.num!=now.num&&sushu[to.num])
{
to.step=now.step;
to.step++;
sushu[to.num]=0;
if(to.num==stop)
{
printf("%d\n",to.step);
return;
}
q.push(to);
}
}
}
printf("Impossible\n");
}
int main()
{
int t;
scanf("%d",&t);
int start,stop;
while(t--)
{
biao();
scanf("%d%d",&start,&stop);
bfs(start,stop);
}
return 0;
}
\