POJ3126-Prime Path

参考:https://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122524.html

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 const int N=1e5+10;
 7 bool vis[N],prime[N];
 8 void dabiao()//打素数表
 9 {
10     memset(prime,true,sizeof(prime));
11     prime[0]=prime[1]=false;
12     for (int i=2;i<N;i++)
13     {
14         if (prime[i])
15         {
16             for (int j=2*i;j<N;j+=i)
17             {
18                 prime[j]=0;
19             }
20         }
21     }
22 }
23 int main()
24 {
25     int n,s,e;
26     dabiao();
27     while (cin>>n)
28     {
29         for (int i=0;i<n;i++)
30         {
31             cin>>s>>e;
32             queue<int> q,a;
33             q.push(s);
34             a.push(0);
35             memset(vis,0,sizeof(vis));
36             vis[s]=1;//做标记
37             int flag=1;
38             while (!q.empty())
39             {
40                 int temp=q.front();
41                 q.pop();
42                 int ans=a.front();
43                 a.pop();
44                 if (temp==e)
45                 {
46                     cout<<ans<<endl;
47                     flag=0;
48                     break;
49                 }
50                 int ge,shi,bai,qian;
51                 qian=temp/1000;//千位
52                 bai=temp%1000/100;//百位
53                 ge=temp%10;//个位
54                 shi=temp%100/10;//十位
55                 for (int i=0;i<=9;i++)
56                 {
57                     int tempp=temp-ge+i;
58                     if (prime[tempp]&&!vis[tempp])
59                     {
60                         q.push(tempp);
61                         a.push(ans+1);
62                         vis[tempp]=1;
63                     }
64                     tempp=qian*1000+bai*100+i*10+ge;
65                     if (prime[tempp]&&!vis[tempp])
66                     {
67                         q.push(tempp);
68                         a.push(ans+1);
69                         vis[tempp]=1;
70                     }
71                     tempp=qian*1000+i*100+shi*10+ge;
72                     if (prime[tempp]&&!vis[tempp])
73                     {
74                         q.push(tempp);
75                         a.push(ans+1);
76                         vis[tempp]=1;
77                     }
78                     if (i>0)//千位不能为0!
79                     {
80                         tempp=i*1000+bai*100+shi*10+ge;
81                         if (prime[tempp]&&!vis[tempp])
82                         {
83                             q.push(tempp);
84                             a.push(ans+1);
85                             vis[tempp]=1;
86                         }
87                     }
88                 }
89             }
90             if (flag)//记得不可能的情况!
91             {
92                 cout<<"Impossible\n";
93             }
94         }
95     }
96 
97     return 0;
98 }

猜你喜欢

转载自www.cnblogs.com/hemeiwolong/p/9452605.html
今日推荐