|
Prime Path
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. 1033The 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 |
题意:
给你n,m,都是四位数,求最少步数使n转化为m.
要求:每次只能改一位数,而且改完后的数必须是素数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;
const int INF = 1000010;
using namespace std;
bool vis[10010],is_prime[10010];
struct node
{
int x;
int num;
};
queue<node>que;
int n,m;
int flag;
void Prime()
{
memset(is_prime,0,sizeof is_prime);
is_prime[0]=is_prime[1]=1;
for(int i=2; i<=10000; i++)
{
if(!is_prime[i])
{
for(int j=i+i; j<=10000; j+=i)
is_prime[j]=1;
}
}
}
int pow_1(int n,int k)
{
int sum=1;
for(int i=0;i<k;i++)
sum*=n;
return sum;
}
void bfs()
{
memset(vis,0,sizeof vis);
while(que.size())
que.pop();
node a,t;
vis[n]=1;
a.x=n;
a.num=0;
que.push(a);
while(que.size())
{
a=que.front();
que.pop();
if(a.x==m)
{
flag=0;
cout<<a.num<<endl;
return;
}
int c[5];
int l=0;
int num=a.x;
while(num)
{
c[l++]=num%10;
num/=10;
}
for(int i=0; i<l; i++)
{
for(int j=0; j<10; j++)
{
int newn=0;
for(int k=0; k<l; k++)
{
if(k==i)//要换的数位
newn+=pow_1(10,k)*j;
else ///其他不变数位
newn+=pow_1(10,k)*c[k];
}
if(newn>=1000&&newn<10000&&!is_prime[newn]&&!vis[newn])
{
t.x=newn,t.num=a.num+1;
que.push(t);
vis[newn]=1;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
Prime();
while(cin>>T)
{
while(T--)
{
cin>>n>>m;
flag=1;
bfs();
if(flag)
cout<<"Impossible\n";
}
}
return 0;
}
原文:http://blog.csdn.net/acm_baihuzi/article/details/42376779