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
题目大意:
给定两个端点,每次变化只能变动某一位上的数,并且变化后的数必须都是素数。问从第一个端点到第二个端点的最少变化次数。
具体思路:
使用bfs,因为都是四位数,所以可以将四位数分别分散开,每一位上都从0跑到9,就判断每一种情况进行遍历。
#include<bits/stdc++.h>
using namespace std;
int a[100000];
int b[100000];
int visited[100000];
void f()
{
for(int i=2; i<=100000; i++)
{
if(a[i]==1)continue;
b[i]=1;
for(int j=i; j<=100000; j+=i)
{
a[j]=1;
}
}
}
struct node
{
int x;
int step;
node() {}
node(int xx,int yy)
{
x=xx;
step=yy;
}
};
void bfs(int w1,int w2)
{
queue<node >q;
node temp1,temp2;
q.push(node(w1,0));
while(!q.empty())
{
temp1=q.front();
q.pop();
if(temp1.x==w2)
{
cout<<temp1.step<<endl;
return ;
}
int t=temp1.x;
int a[5];
for(int i=1; i<=4; i++)
{
//cout<<2<<endl;
a[i]=t%10;
t=t/10;
}//将每一位数都分散开
int temp=temp1.x;
int times=1;
for(int i=1; i<=4; i++)
{
//cout<<3<<endl;
for(int j=0; j<10; j++)
{
//cout<<4<<endl;
if(a[i]==j)continue;如果是和原来的数相同,就不进行入栈的操作
if(i==4&&j==0)continue;//判断千位上的数,注意千位不能是0
int sum=temp-a[i]*times+j*times;
if(visited[sum]==0&&b[sum]==1)
{
q.push(node(sum,temp1.step+1));
}
visited[sum]=1;//这个地方一定要注意,即使没有入栈,也必须将遍历过的数标记,否则就会陷入死循环
}
times*=10;
}
}
}
int main()
{
int t;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
f();
cin>>t;
int a,b;
while(t--)
{
memset(visited,0,sizeof(visited));
cin>>a>>b;
bfs(a,b);
}
return 0;
}
反思:以后再也不敢随便立flag了,太刺激了,搞的现在吹空调吹得膀子疼,,,,,