Prime Path
Time Limit: 1000MS Memory Limit: 65536K
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
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
6
7
0
题目大意就是输入两个四位素数数,每次改变前面的素数的一位,改变后的数也要是素数,然后再改变变后的苏素数的一位。问最小多少步可以变成后面那个素数。例如样例1:
1033-->1733-->3733-->3739-->3779-->8779-->8179 一共六步。。
这一题用广搜。同时用素数表来判断一个数是否为素数。。模拟操作的过程(改变个位、改变百位……)。
代码:
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
bool visit[10000]; //判断i(visit[i])是否已经被访问过。
bool prime[10000]; //判断i (prime[i]) 是否为素数。
int cost[10000]; //判断变成 i (cost[i])时 花费多少钱。
int bfs(int x,int y)
{
queue<int> q; //建立一个int型的队列。
q.push(x); //将x压入队列。
visit[x]=true; //初始化 x被访问过。
while(!q.empty())
{
int now=q.front(); //将q的队头赋值给now。
q.pop();
for(int i=0;i<=9;i++)
{
int temp=(now/10)*10+i; //改变个位。
if(prime[temp]==true && visit[temp]==false)
{
visit[temp]=true;
q.push(temp);
cost[temp]=cost[now]+1;
}
temp=(now/100)*100+i*10+now%10; //改变十位。
if(prime[temp]==true && visit[temp]==false)
{
visit[temp]=true;
q.push(temp);
cost[temp]=cost[now]+1;
}
temp=(now/1000)*1000+i*100+now%100; //改变千位。
if(prime[temp]==true && visit[temp]==false)
{
visit[temp]=true;
q.push(temp);
cost[temp]=cost[now]+1;
}
temp=now%1000+i*1000; //改变万位。
if(i!=0 && prime[temp]==true && visit[temp]==false)
{
visit[temp]=true;
q.push(temp);
cost[temp]=cost[now]+1;
}
if(visit[y]) //表示变成了y.
return cost[y];
}
}
return 0;
}
int main()
{
int i,j,k;
memset(prime,true,sizeof(prime));
for( i=2;i<=10000;i++) //建立素数表。
{
if(prime[i])
{
for(int j=i;j<=10000/i;j++)
prime[i*j]=false; //这个很巧妙,排除不是素数的,true表示是素数。
}
}
int n,num1,num2;
cin>>n;
for(i=0;i<n;i++)
{
memset(visit,false,sizeof(visit));
memset(cost,0,sizeof(cost));
cin>>num1>>num2;
cout<<bfs(num1,num2)<<endl;
}
return 0;
}
1530

被折叠的 条评论
为什么被折叠?



