POJ 3126 Prime Path

本文介绍了一个结合广度优先搜索(BFS)与素数筛算法的应用案例,通过具体的代码实现展示了如何解决特定问题。该算法用于寻找两个素数之间的最短转换路径,通过替换数字位来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  简单的BFS + 素数筛。。。。。手残把素数筛敲错了  调试了大半年......不过终于迎来了久违的 1A

  

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <queue>
  7 
  8 using namespace std;
  9 
 10 bool HashPrime[10010];
 11 bool Mark[10010];
 12 
 13 struct N
 14 {
 15     int p,s;
 16 }t,nt;
 17 
 18 void bfs(int n,int m)
 19 {
 20     int i;
 21     queue<N> q;
 22     t.p = n;
 23     t.s = 0;
 24     q.push(t);
 25     Mark[n] = true;
 26 
 27     while(!q.empty())
 28     {
 29         t = q.front();
 30         q.pop();
 31 
 32         if(t.p == m)
 33         {
 34             printf("%d\n",t.s);
 35             return ;
 36         }
 37         for(i = 1;i < 10; ++i)
 38         {
 39             nt.s = t.s+1;
 40             nt.p = t.p/10*10 + i;
 41 
 42             if(HashPrime[nt.p] == false && Mark[nt.p] == false)
 43             {
 44                 q.push(nt);
 45                 Mark[nt.p] = true;
 46             }
 47         }
 48         for(i = 0;i < 10; ++i)
 49         {
 50             nt.s = t.s+1;
 51             nt.p = t.p/100*100 + t.p%10 + i*10;
 52 
 53             if(HashPrime[nt.p] == false && Mark[nt.p] == false)
 54             {
 55                 q.push(nt);
 56                 Mark[nt.p] = true;
 57             }
 58         }
 59         for(i = 0;i < 10; ++i)
 60         {
 61             nt.s = t.s+1;
 62             nt.p = t.p/1000*1000 + t.p%100 + i*100;
 63 
 64             if(HashPrime[nt.p] == false && Mark[nt.p] == false)
 65             {
 66                 q.push(nt);
 67                 Mark[nt.p] = true;
 68             }
 69         }
 70         for(i = 1;i < 10; ++i)
 71         {
 72             nt.s = t.s+1;
 73             nt.p = t.p%1000 + i*1000;
 74 
 75             if(HashPrime[nt.p] == false && Mark[nt.p] == false)
 76             {
 77                 q.push(nt);
 78                 Mark[nt.p] = true;
 79             }
 80         }
 81     }
 82     printf("Impossible\n");
 83     return ;
 84 }
 85 
 86 int main()
 87 {
 88     int i,j;
 89     memset(HashPrime,false,sizeof(HashPrime));
 90     for(i = 2;i <= 10000; ++i)
 91     {
 92         if(HashPrime[i] == false)
 93         {
 94             for(j = i+i;j <= 10000; j += i)
 95             {
 96                 HashPrime[j] = true;
 97             }
 98         }
 99     }
100 
101     int n,m;
102     int T;
103     scanf("%d",&T);
104     while(T--)
105     {
106         memset(Mark,false,sizeof(Mark));
107         scanf("%d %d",&n,&m);
108         bfs(n,m);
109     }
110 
111     return 0;
112 }

 

转载于:https://www.cnblogs.com/zmx354/p/3270384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值