F - Prime Path
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
题解:
一共不到10000个数,暴力枚举每个数就好,是素数的话推进队列里。
TLE了一次啊。加深了对bfs的理解。
四状态Bfs+筛法素数表。
对每个已访问过的状态我们不应该再次访问,加个vis数组就过了。
注意千位不能出现0的情况。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct Node
{
int data,times;
Node(int d,int ts):data(d),times(ts) {}
};
const int MAXN = 10000;
bool vis[MAXN];
bool notprime[MAXN];
void init()
{
memset(notprime,false,sizeof(notprime));
notprime[0]=notprime[1]=true;
for(int i=2;i<MAXN;i++)
{
if(!notprime[i])
{
if(i>MAXN/i)
continue;
}
for(int j=i*i;j<MAXN;j+=i)
{
notprime[j]=true;
}
}
}
int a,b;
void bfs()
{
queue<Node> que;
que.push(Node(a,0));
while(que.size())
{
Node p = que.front();
que.pop();
if(p.data==b)
{
//printf("%d\n",p.data);
printf("%d\n",p.times);
return;
}
int gewei = p.data%10;
int shiwei = p.data/10%10;
int baiwei = p.data/100%10;
int qianwei = p.data/1000;
//个位的变换
for(int i=1;i<10;i++)
{
int tmp = qianwei*1000+baiwei*100+shiwei*10+(gewei+i)%10;
if(!notprime[tmp]&&!vis[tmp])
{
vis[tmp]=1;
que.push(Node(tmp,p.times+1));
}
}
//十位的变换
for(int i=1;i<10;i++)
{
int tmp = qianwei*1000+baiwei*100+(shiwei+i)%10*10+gewei;
if(!notprime[tmp]&&!vis[tmp])
{
vis[tmp]=1;
que.push(Node(tmp,p.times+1));
}
}
//百位的变换
for(int i=1;i<10;i++)
{
int tmp = qianwei*1000+(baiwei+i)%10*100+shiwei*10+gewei;
if(!notprime[tmp]&&!vis[tmp])
{
vis[tmp]=1;
que.push(Node(tmp,p.times+1));
}
}
//千位的变换
for(int i=1;i<10;i++)
{
if(qianwei+i==10) continue;
int tmp = (qianwei+i)%10*1000+baiwei*100+shiwei*10+gewei;
if(!notprime[tmp]&&!vis[tmp])
{
vis[tmp]=1;
que.push(Node(tmp,p.times+1));
}
}
}
printf("Impossible.\n");
}
int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
memset(vis,false,sizeof(vis));
scanf("%d%d",&a,&b);
bfs();
}
return 0;
}