POJ 3126 BFS

题意

将一个四位的素数,变为另一个四位的素数。每次只能换一位上的数字,不能存在前导0,且中间过程中,换掉一位数字后的数也都是素数。

问最少变换的次数

分析

bfs即可

代码

 1 /* When all else is lost the future still remains. */
 2 /* You can be the greatest */
 3 #define rep(X,Y,Z) for(int X=(Y);X<(Z);X++)
 4 #define drep(X,Y,Z) for(int X=(Y);X>=(Z);X--)
 5 #define fi first
 6 #define se second
 7 #define mk(X,Y) make_pair((X),(Y))
 8 #define inf 0x3f3f3f3f
 9 #define clr(X,Y) memset(X,Y,sizeof(X))
10 #define pb push_back
11 //head
12 #include <iostream>
13 #include <stdio.h>
14 #include <queue>
15 #include <algorithm>
16 #include <string>
17 #include <map>
18 #include <string.h>
19 using namespace std;
20 bool primer[10010];
21 int val[10010];
22 bool cur[10010];
23 bool is(int in){
24     for(int i = 2 ; i * i <= in ; i++)
25         if(in % i == 0) return 0;
26     return 1;
27 }
28 void init(){
29     clr(primer,0);
30     rep(i,1000,10000) if(is(i)) primer[i] = 1;
31     return ;
32 }
33 int num(int in[5]){
34     return in[0]*1000+in[1]*100+in[2]*10+in[3];
35 }
36 void slove(int be , int end){
37     clr(val,0);
38     clr(cur,0);
39     //
40     queue<int> Q;
41     Q.push(be);
42     //
43     while(!Q.empty()){
44         if(cur[end]) break;
45         int now = Q.front();
46         cur[now] = 1;
47         int t = now;
48         Q.pop();
49         int p[5];
50         drep(i,3,0){
51             p[i] = t % 10;
52             t /= 10;
53         }
54         rep(i,0,4){
55             int a = p[i];
56             rep(j,0,10){
57                 if(!i && !j) continue;
58                 if(a == j) continue;
59                 p[i] = j;
60                 int k = num(p);
61                 if(!primer[k]) continue;
62                 if(cur[k]) continue;
63                 val[k] = val[now] + 1;
64                 cur[k] = 1;
65                 Q.push(k);
66             }
67             p[i] = a;
68         }
69     }
70     return;
71 }
72 int main(){
73     init();
74     int n;
75     while(~scanf("%d",&n)) while(n--){
76         char s1[5],s2[5];
77         int pre[5],after[5];
78         scanf("%s %s",s1,s2);
79         //
80         rep(i,0,4){
81             pre[i] = s1[i] - ‘0‘;
82             after[i] = s2[i] - ‘0‘;
83         }
84         //printf("%d %d\n",num(pre),num(after));
85         slove(num(pre),num(after));
86         //
87         if(!cur[num(after)]) printf("Impossible\n");
88         else printf("%d\n",val[num(after)]);
89     }
90     return 0;
91 }
时间: 2024-11-07 00:51:08

POJ 3126 BFS的相关文章

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

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(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

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

20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple

>>>>>>>>>POJ 1426直达?? >>>>>>>>>POJ 3126直达?? 做了这么几道搜索题,感觉差不多也摸出了门路,模板差不多记下来了,只是面对不同的题目算法不同罢了 简单写一下想法,搞明白搜索的主题到底是什么(之前的mutiple那题,一开始就没想明白到底搜谁,就没想到算法 TBC(晚上再写 ===================================分割线=======

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM