POJ3126Prime 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.
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
题意
(看这么长的题,这么长这么长,还是对话,真是奇奇怪怪的,我居然还,读完了)
就是给两个素数
问从第一个数变到第二个数需要几步
每次变都只能变一位数,并且变成的那个数也得是个素数
(这人sjb吧)
思路
对一个数字,把每一位变成0,1,2…8,9
进行判断
就是一个,emmm,奇怪的bfs
它不在搜索专题里,我应该是想不到这个要用bfs的,就酱~
(and这个要用到素数筛)
AC代码
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define EPS 1e-6
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long
#define mian main
bool flag[10010];//是否素数
bool vis[10010];//是否访问
struct node
{
int a, b, c, d;//千百十个位
int s;//步数
}re, pre, to;
queue<node> q;
int dir[8][4] = {0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0, 0,0,0,-1, 0,0,-1,0, 0,-1,0,0, -1,0,0,0};//就每一位的变化趋势
void prime()
{
memset(flag, 1, sizeof(flag));
flag[0] = 0;
flag[1] = 0;
for(int i = 2; i <= 10000; i++)
{
if(flag[i])//是素数的话
{
for(int j = i * i; j <= 10000; j += i)
{
flag[j] = 0;//他所有的倍数都不是素数
}
}
}
}
int bfs()
{
while(!q.empty())
{
node s;
s = q.front();
q.pop();
if(s.a == to.a && s.b == to.b && s.c == to.c &&s.d == to.d)
return s.s;
for(int i = 0; i < 8; i ++)
{
for(int j = 1; j <= 9; j ++)
{
pre.a = s.a + j * dir[i][0];
pre.b = s.b + j * dir[i][1];
pre.c = s.c + j * dir[i][2];
pre.d = s.d + j * dir[i][3];
int sz = pre.a * 1000 + pre.b * 100 + pre.c * 10 + pre.d;
pre.s = s.s + 1;
if(pre.a > 0 && pre.a <= 9 && pre.b >= 0 && pre.b <= 9 && pre.c >= 0 && pre.c <= 9 &&pre.d>= 0 && pre.d <= 9 && !vis[sz] && flag[sz])//注意千位不能是0
{
//cout<<pre.a<<' '<<pre.b<< ' '<<pre.c<<' '<<pre.d<<' '<<pre.s<<endl;
q.push(pre);
vis[sz] = 1;
}
}
}
}
}
int main()
{
int c;
cin>>c;
int f, t;
prime();
while(c--)
{
mem(vis, 0);
while(!q.empty())
q.pop();
cin>>f>>t;
re.a = f / 1000;
re.b = (f % 1000) / 100;
re.c = (f % 100) / 10;
re.d = f % 10;
to.a = t / 1000;
to.b = (t % 1000) / 100;
to.c = (t % 100) / 10;
to.d = t % 10;
//cout<<re.a<<' '<<re.b<< ' '<<re.c<<' '<<re.d<<endl;
//cout<<to.a<<' '<<to.b<< ' '<<to.c<<' '<<to.d<<endl;
re.s = 0;
q.push(re);
vis[f] = 1;
int ans = bfs();
cout<<ans<<endl;
}
return 0;
}