POJ 3126 Prime Path

水题:直接判断素数+bfs

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <sstream>
 5 #include <algorithm>
 6 #include <list>
 7 #include <map>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <cmath>
12 #include <cstdlib>
13 //#include <memory.h>
14 #define clc(a,b) memset(a,b,sizeof(a))
15 using namespace std;
16 const int maxn=100000;
17 const int inf=0x3f3f3f3f;
18 int n,m;
19 bool pri[10000],vis[10000];
20 int countt[10000];
21 int t[4];
22 void prim(){
23     for(int i=1000;i<10000;i++){
24         bool flag=false;
25         for(int j=2;j<i;j++)
26             if(i%j==0){
27                pri[i]=false;
28                flag=true;
29                break;
30             }
31         if(!flag)
32             pri[i]=true;
33     }
34 }
35
36 int dfs(int a,int b){
37     queue<int>q;
38     q.push(a);
39     vis[a]=true;
40     countt[a]=0;
41     while(!q.empty()){
42         int v=q.front();
43         q.pop();
44         if(v==b){
45             return countt[v];
46         }
47         t[0]=v/1000;
48         t[1]=v%1000/100;
49         t[2]=v%100/10;
50         t[3]=v%10;
51         for(int i=0;i<4;i++){
52            int tem=t[i];
53            for(int j=0;j<10;j++){
54               if(j!=tem){
55                 t[i]=j;
56                 int vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];
57                 if(pri[vtemp]&&!vis[vtemp]){
58                    countt[vtemp]=countt[v]+1;
59                    vis[vtemp]=true;
60                    q.push(vtemp);
61                 }
62                 if(vtemp==b){
63                     return countt[vtemp];
64                 }
65               }
66            }
67            t[i]=tem;
68         }
69     }
70     return -1;
71 }
72 int main(){
73     // freopen("in.txt","r",stdin);
74     int ans;
75     int T;
76     while(~scanf("%d",&T)){
77         prim();
78         while(T--){
79            scanf("%d%d",&n,&m);
80            clc(vis,false);
81            clc(countt,0);
82            ans=dfs(n,m);
83            if(ans!=-1)
84             printf("%d\n",ans);
85            else
86             printf("Impossible\n");
87        }
88     }
89     return 0;
90 }

时间: 2024-10-09 23:43:39

POJ 3126 Prime Path的相关文章

双向广搜 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】

题意:给你一个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

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

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

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