kuangbin專題 專題一 簡單搜索 Prime Path POJ - 3126


題目連接:https://vjudge.net/problem/POJ-3126

題意:給你兩個四位的素數N,M,每次改變N四位數中的其中一位,若是能通過有限次數的替換變成四位數M,那麼求出最少替換次數,不然輸出「Impossible」.(N,M必須一直是素數)node

思路:bfs。四位數,每一位能夠替換爲0~9,那麼咱們能夠每次改變N中的一位數,而後放入隊列中,固然,在替換數字時不免會出現重複的四位數,這樣會形成TLE,那麼咱們能夠建立一個bool數組標記出現過的,咱們也須要素數篩999 ~ 10000之間的素數(你想刪哪裏到哪就到哪裏,不要糾結),由於是bfs,因此第一次出現的新的四位素數必定是替換次數最少的,那麼題目就簡單了。ios


 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;  7 
 8 #define inf (1LL << 31) - 1
 9 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
 10 #define rep__(i,j,k) for(int i = (j); i < (k); i++)
 11 #define per(i,j,k) for(int i = (j); i >= (k); i--)
 12 #define per__(i,j,k) for(int i = (j); i > (k); i--)
 13 
 14 const int N = (int)1e4 + 10;  15 bool vis[N]; //素數表 0爲素數
 16 bool app[N]; //標記是否出現過
 17 int ans;  18 
 19 struct node{  20 
 21     int a[4];  22     int cost;  23 
 24     node(int* a,int e){  25         rep(i, 0, 3){  26             this->a[i] = a[i];  27  }  28         this->cost = e;  29  }  30 
 31     int x(){ //返回四位數的成員函數
 32         int num = 0;  33         rep(i, 0, 3) num = num * 10 + a[i];  34         return num;  35  }  36 };  37 
 38 void get_Prime(){ //素數打表
 39 
 40     rep(i, 2, (int)sqrt(N*1.0)){  41         if (!vis[i]){  42             for (int p = i * i; p <= N; p += i) vis[p] = true;  43  }  44  }  45 }  46 
 47 bool work(int x[], int y){  //true爲有答案,false爲沒答案
 48 
 49     queue<node> que;  50     node t (x,0);  51 
 52     app[t.x()] = true;  53 
 54  que.push(t);  55 
 56     if (t.x() == y){  57         ans = 0;  58         return true;  59  }  60 
 61     while (!que.empty()){  62 
 63         node tmp = que.front();  64  que.pop();  65 
 66         rep(i, 0, 3){ //1~4不一樣位置
 67             rep(j, 0, 9){ //替換爲0~9
 68                 if (i == 0 && j == 0) continue; //第一位不能是0
 69                 int tt = tmp.a[i]; //暫存該數
 70                 tmp.a[i] = j;      //改變  71                 
 72                 //該四位數沒有出現過且該數是素數
 73                 if (!app[tmp.x()] && !vis[tmp.x()]){  74 
 75                     app[tmp.x()] = true; //標記一下
 76 
 77                     if (tmp.x() == y){ //若是變成了想變成的數了
 78                         ans = tmp.cost + 1;  79                         return true;  80  }  81                     que.push(node{tmp.a,tmp.cost + 1}); //新的四位數放入隊列,花費加一
 82  }  83                 tmp.a[i] = tt; //變回原來的四位數
 84  }  85  }  86 
 87  }  88 
 89     return false;  90 }  91 
 92 int main(){  93 
 94     ios::sync_with_stdio(false);  95     cin.tie(0);  96 
 97     get_Prime();//獲得素數表
 98     int n;  99     cin >> n; 100 
101     int a, b; 102     while (n--){ 103 
104         memset(app, 0, sizeof(app)); //每次初始化
105 
106         cin >> a >> b; 107 
108         int aa[4]; 109         int len = 0; 110         rep(i, 0, 3){ 111             aa[3-len++] = a % 10; 112             a /= 10; 113         } //分割a變爲四個數 114 
115         //node tmp(aa, 0); 116         //cout << "tmp:::" << tmp.x() << endl;
117 
118         if (work(aa, b)) cout << ans << endl; 119         else cout << "Impossible" << endl; 120  } 121     
122     return 0; 123 }
相關文章
相關標籤/搜索