POJ 3216 Prime Path(数字BFS)

Prime Path
 

大意:给你两个数,求从第一个数经过几次变换到第二个数。变换要求:1.中间数必须是素数   2.每次只能变一个数字。

 

思路:对每一位数字进行bfs

 
 
  1 #include <map>
  2 #include <stack>
  3 #include <queue>
  4 #include <math.h>
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <iostream>
  8 #include <limits.h>
  9 #include <algorithm>
 10 #define LL long long
 11 #define min(a,b) (a>b?b:a)
 12 #define max(a,b) (a>b?a:b)
 13 #define eps 1e-9
 14 #define INF 1 << 30
 15 using namespace std;
 16 
 17 int a, b, Ans;
 18 bool flag[10000];
 19 int vis[10000] = {0};
 20 void prime()
 21 {
 22     int k, j;
 23     for(int i = 1001; i < 10000; i+=2)
 24     {
 25         k = sqrt(i*1.0);
 26         for(j = 2; j <= k; j++)
 27             if(i % j == 0)
 28                 break;
 29         if(j >= k + 1)
 30             flag[i] = 1;
 31     }
 32 }
 33 
 34 void BFS()
 35 {
 36     int t;
 37     int i;
 38     int p1, p2, p3;
 39     queue<int> q;
 40     q.push(a);
 41     vis[a] = true;
 42     p1 = 0;
 43     p2 = p3 = 1;    
 44     while (!q.empty())
 45     {
 46         t = q.front();
 47         q.pop();
 48         p1++;
 49         if (t == b)
 50             return;
 51         int next;
 52         for (i = 1; i <= 9; i++)
 53         {
 54             next = t%1000;
 55             next += i*1000;
 56             if (flag[next] && !vis[next])
 57             {
 58                 vis[next] = true;
 59                 q.push(next);
 60                 p2++;
 61             }
 62         }
 63         for (i = 0; i <= 9; i++)
 64         {
 65             int temp;
 66             temp = t/100%10;
 67             next = t-temp*100;
 68             next += i*100;
 69             if (flag[next] && !vis[next])
 70             {
 71                 vis[next] = true;
 72                 q.push(next);
 73                 p2++;
 74             }
 75         }
 76         for (i = 0; i <= 9; i++)
 77         {
 78             int temp;
 79             temp = t/10%10;
 80             next = t-temp*10;
 81             next += i*10;
 82             if (flag[next] && !vis[next])
 83             {
 84                 vis[next] = true;
 85                 q.push(next);
 86                 p2++;
 87             }
 88         }
 89         for (i = 0; i <= 9; i++)
 90         {
 91             int temp;
 92             temp = t%10;
 93             next = t-temp;
 94             next += i;
 95             if (flag[next] && !vis[next])
 96             {
 97                 vis[next] = true;
 98                 q.push(next);
 99                 p2++;
100             }
101         }
102         if (p1 == p3)
103         {
104             Ans++;
105             p3 = p2;
106         }
107     }
108 }
109 
110 void Solve()
111 {
112     int n;
113     cin >> n;
114     while(n--)
115     {
116         cin >> a >> b;
117         memset(vis, 0, sizeof(vis));
118         Ans = 0;
119         BFS();
120         cout << Ans << endl;
121     }
122 }
123 
124 int main(void)
125 {
126     //freopen("data.in", "r", stdin);
127     //freopen("data.out", "w", stdout);
128     prime();
129     Solve();
130 
131     return 0;
132 }
Prime Path

 

posted @ 2013-12-25 16:53  GLSilence  阅读(272)  评论(0编辑  收藏  举报