搜索------prime path

给定两个数a,b,每次只能变动a的其中一个数,变成的数也必须是素数,问最少经过几次可以变成b;

----------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define MAXV 11000
bool prime[MAXV];
void init()//判断是否素数
{
int i,j;
for(i=1000; i<=MAXV; i++)
{
for(j=2; j<i; j++)
if(i%j==0)
{
prime[i]=false;
break;
}
if(j==i) prime[i]=true;
}
}
int bfs(int first,int last)
{
bool dis[MAXV];
queue <int>q;
int v,i,j,temp,vtemp,count[MAXV],t[4];
memset(dis,false,sizeof(dis));
memset(count,0,sizeof(count));

q.push(first);
dis[first]=true;

while(!q.empty())
{
v=q.front();
q.pop();

t[0]=v/1000;
t[1]=v%1000/100;
t[2]=v%100/10;
t[3]=v%10;
// printf("%d %d %d %d",t[0],t[1],t[2],t[3]);

for(j=0; j<4; j++)
{
temp=t[j];
for(i=0; i<10; i++)
if(i!=temp)
{
t[j]=i;
vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];
if(!dis[vtemp] && prime[vtemp])
{
count[vtemp]=count[v]+1;
dis[vtemp]=true;
q.push(vtemp);
}
if(vtemp==last) return count[vtemp];
}
t[j]=temp;
}
if(v==last) return count[v];
}
return -1;
}
int main()
{
int n,a,b,key;
init();
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
key=bfs(a,b);
if(key!=-1) printf("%d\n",key);
else printf("Impossible\n");
}
return 0;
}

时间: 2024-10-06 07:53:47

搜索------prime path的相关文章

poj 3126 Prime Path(搜索专题)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 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-di

Prime Path (poj 3126 bfs)

Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 6640 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to c

F - Prime Path POJ 3126 筛选素数+bfs

F - Prime Path Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3126 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have t

POJ3126 Prime Path 打表+BFS

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

HDU 1976 prime path

题意:给你2个数n m,从n变成m最少需要改变多少次. 其中: 1.n  m  都是4位数 2.每次只能改变n的一个位数(个位.十位.百位.千位),且每次改变后后的新数为素数 思路:搜索的变形题,这次我们要搜得方向是改变位数中的一位,然后往下搜,直到求出我们需要的那个解 #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> us

poj3126——Prime Path(BFS)

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

HDU - 1973 - Prime Path (BFS)

Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 987    Accepted Submission(s): 635 Problem Description The ministers of the cabinet were quite upset by the message from the Chief of S

Prime Path POJ-3126

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

(简单) POJ 3126 Prime Path,BFS。

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