POJ - 3126 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
题解
万万没想到这题也是bfs,hhhh,要是不在搜索的栏目里面我还真不知道怎么写。
因为数字都是四位数,这样就好写多了啊。
直接开个结构体储存一下就好啦,然后就是bfs模板题。
/*************************************************************************
> Problem: POJ - 3126 Prime Path
> Author: ALizen_
> Mail: hhu_zyh@qq.com
> Blog: https://blog.youkuaiyun.com/Jame_19?spm=1001.2101.3001.5343
*************************************************************************/
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define slf(x) scanf("%lld",&x)
#define plf(x) printf("%lld\n",x)
#define MEM(a,b) memset((a),(b),sizeof(a))
#define ref(i,a,b) for(re i = a ; i >= b ; -- i)
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
using namespace std;
typedef long long LL;
int em,en,ep,eq;
int ans=100000;
int book[15][15][15][15];
struct node{
int m,n,p,q,step;
};
bool JudgePrime(int digit){
if(digit==2 || digit==3)
return true;
else if(digit<=1 || digit%2==0)
return false;
else if(digit>3)
{
for(int i=3;i*i<=digit;i+=2)
if(digit%i==0)
return false;
return true;
}
}
bool check(int m,int n,int p,int q){
int sum=m*1000+n*100+p*10+q;
if(JudgePrime(sum)&&!book[m][n][p][q])
return true;
else
return false;
}
node now,next,start;
void bfs(){
queue<node> q;
q.push(start);
while(q.size()){
now=q.front();
q.pop();
for(int i=1;i<=9;i++){
if(i==now.m)continue;
next.m=i;
next.n=now.n;
next.p=now.p;
next.q=now.q;
next.step=now.step+1;
if(check(next.m,next.n,next.p,next.q)){
q.push(next);
book[next.m][next.n][next.p][next.q]=1;
if(next.m==em&&next.n==en&&next.p==ep&&next.q==eq){
ans=min(ans,next.step);
}
}
}
for(int i=0;i<=9;i++){
if(i==now.n)continue;
next.m=now.m;
next.n=i;
next.p=now.p;
next.q=now.q;
next.step=now.step+1;
if(check(next.m,next.n,next.p,next.q)){
q.push(next);
book[next.m][next.n][next.p][next.q]=1;
if(next.m==em&&next.n==en&&next.p==ep&&next.q==eq){
ans=min(ans,next.step);
}
}
}
for(int i=0;i<=9;i++){
if(i==now.p)continue;
next.m=now.m;
next.n=now.n;
next.p=i;
next.q=now.q;
next.step=now.step+1;
if(check(next.m,next.n,next.p,next.q)){
q.push(next);
book[next.m][next.n][next.p][next.q]=1;
if(next.m==em&&next.n==en&&next.p==ep&&next.q==eq){
ans=min(ans,next.step);
}
}
}
for(int i=0;i<=9;i++){
if(i==now.q)continue;
next.m=now.m;
next.n=now.n;
next.p=now.p;
next.q=i;
next.step=now.step+1;
if(check(next.m,next.n,next.p,next.q)){
q.push(next);
book[next.m][next.n][next.p][next.q]=1;
if(next.m==em&&next.n==en&&next.p==ep&&next.q==eq){
ans=min(ans,next.step);
}
}
}
}
}
int main(){
int t;cin>>t;
while(t--){
memset(book,0,sizeof(book));
ans=100000;
int sx,ex;
cin>>sx>>ex;
start.m=sx/1000;start.n=sx/100%10;
start.p=sx/10%10;start.q=sx%10;
start.step=0;book[start.m][start.n][start.p][start.q]=1;
em=ex/1000;en=ex/100%10;
ep=ex/10%10;eq=ex%10;
if(start.m==em&&start.n==en&&start.p==ep&&start.q==eq){
cout<<0<<endl;
continue;
}
bfs();
if(ans==100000)cout<<"Impossible"<<endl;
else cout<<ans<<endl;
}
return 0;
}