经验就是要认真细心,要深刻理解。num
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<queue>
using namespace std;
#define N 11000
typedef struct yes
{
int l, m;//location, money;
}YES;
YES s, e, p, q;
int isprime(int m);
bool visit[N];
void BFS();
int main()
{
int n;//number
cin >> n;
while(n--)
{
cin >> s.l >> e.l;
s.m=0;
BFS();
}
return 0;
}
void BFS()
{
int i, j, num, figure;
char s1[5], s2[5];
memset(visit, false, sizeof(visit));
queue<YES>que;
que.push(s);
visit[s.l]=true;
while(que.size())
{
p=que.front();
que.pop();
if(p.l==e.l)
{
cout << p.m << endl;
return;
}
figure=p.l;
for(i=3; i>=0; i--)
{
s1[i]=figure%10+‘0‘;
figure/=10;
}
s1[4]=‘\0‘;
for(i=0; i<4; i++)
{
strcpy(s2, s1);
for(j=0; j<=9; j++)
{
if((i==0&&j==0)||s2[i]==j+‘0‘)
continue;
s2[i]=j+‘0‘;
num=atoi(s2);
if(num&1==0)
continue;
//if(num%2==0)
//continue;
if(isprime(num)&&!visit[num])
{
visit[num]=true;
q.l=num;
q.m=p.m+1;
que.push(q);
}
}
}
}
cout << "Impossible" << endl;
}
int isprime(int m)
{
int i;
int k=sqrt(m);
k=int(k);
for(i=2; i<=k; i++)
if(m%i==0)
return 0;
return 1;
}