BFS/poj 3126 prime path

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 int n,x,y;
 7 bool v[20000];
 8 struct point
 9 {
10     int x;
11     int step;
12 };
13
14 int change(int x,int y,int z)
15 {
16     if (z==4) return (x/10)*10+y;
17     else if (z==3)
18     {
19         int t=y*10+x%10;
20         return (x/100)*100+t;
21     }
22     else if (z==2)
23     {
24         int t=y*100+x%100;
25         return (x/1000)*1000+t;
26     }
27     else if (z==1)
28     {
29         return y*1000+(x%1000);
30     }
31 }
32
33 bool prime(int x)
34 {
35     if (x==2 || x==3) return true;
36     if (x<=1 || x%2==0) return false;
37     for (int i=3;i<=sqrt(x);i++)
38         if (x%i==0) return false;
39     return true;
40 }
41
42 void bfs(int sx,int ex)
43 {
44     memset(v,0,sizeof(v));
45     queue<point>que;
46     point now,next;
47     now.x=sx;
48     now.step=0;
49     v[sx]=1;
50     que.push(now);
51     while (!que.empty())
52     {
53         now=que.front();
54         que.pop();
55         if (now.x==ex)
56         {
57             printf("%d\n",now.step);
58             return ;
59         }
60
61         for (int i=0;i<=9;i++)
62             for (int j=1;j<=4;j++)
63             {
64                 if (i==0 && j==1) continue;
65                 next.x=change(now.x,i,j);
66                 if (prime(next.x) && !v[next.x])
67                 {
68                     v[next.x]=1;
69                     next.step=now.step+1;
70                     que.push(next);
71                 }
72             }
73
74     }
75     printf("Impossible\n");
76 }
77 int main()
78 {
79     scanf("%d",&n);
80     for (int t=1;t<=n;t++)
81     {
82         scanf("%d%d",&x,&y);
83         bfs(x,y);
84     }
85     return 0;
86 }
时间: 2024-10-14 01:51:00

BFS/poj 3126 prime path的相关文章

poj 3126 Prime Path 【暴力BFS】

题意:给你一个4位数,再给你一个4位数,如前一个数的每次只移动一位,问你能不能将第一个数变成第二个. 转移条件:1,只能通过素数作为中转,2,每次移动一位. 如果找到输出最少的转移次数(或步数), 如果找不到输出Impossible. 策略:如题. 直接上代码: #include<stdio.h> #include<string.h> #include<queue> #define M 10005 using std::queue; int vis[10000]; in

双向广搜 POJ 3126 Prime Path

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 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 th

POJ 3126 Prime Path(素数路径)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 3126 Prime Path SPFA

http://poj.org/problem?id=3126 题目大意: 给你两个四位的素数s和t,要求每次改变一个数字,使得改变后的数字也为素数,求s变化到t的最少变化次数. 思路: 首先求出所有4位素数. 对于两个素数之间,如果只相差一个数字,那么就建立图,(双向) 最后求最短路即可(可以SPFA也可以BFS) #include<cstdio> #include<cstring> #include<queue> #include<algorithm> u

POJ - 3126 - Prime Path(BFS)

Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数的最小值.(a,b,c都是四位数字,输入时没有前导零) 分析: 每次改变可以获得一个四位数c,然后如果c是素数并且之前没有出现过,那么我们把它放入队列即可. int f[10001]; int v[10001]; void init()//素数筛 { memset(f,0,sizeof f); fo

(简单) 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

poj 3126 Prime Path (bfs)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13813   Accepted: 7796 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-dig

POJ 3126 Prime Path(BFS)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6843 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-dig

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