poj 3126 Prime Path 【bfs】

题目地址:http://poj.org/problem?id=3126

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0
分析:从一个四位素数a 变到另一个四位素数b,求最少的变换次数。bfs求最少的次数!(变换的要求请读题目)
一开始在记录一个数是否被访问过的时候,用了一种比较的方式,即:比较当前要生成的数和cur是否相同,这种
判断是否该数字是否访问过的方法是不对的,因为这样判断还是会导致一个数可能会被多次加入队列,最后TLE。
故,改为vis[]标记判断是否访问过,这样每个数顶多会被加入队列一次。AC!
代码:
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <ctype.h>
  5 #include <math.h>
  6 #include <cmath>
  7 #include <iostream>
  8 #include <string>
  9 #include <queue>
 10 #include <stack>
 11 #include <vector>
 12 #include <algorithm>
 13 #define N 100000+100
 14 
 15 using namespace std;
 16 
 17 struct node
 18 {
 19     int a, b, c, d; //a不能等于0
 20     int path;
 21 };
 22 
 23 int f[10010];
 24 bool vis[10010];
 25 
 26 void sushu()
 27 {
 28     int i, dd=sqrt(10000+0.5);
 29     memset(f, 0, sizeof(f));
 30     i=2; f[1]=f[0]=1; //not
 31     while(i<=dd){
 32         for(int j=i+i; j<=10000; j+=i)
 33             f[j]=1; // not
 34         i++;
 35         while(f[i]==1) i++;
 36     }
 37 }
 38 
 39 int main()
 40 {
 41     sushu(); //筛素数
 42    // printf("%d  %d", f[1001], f[1003] );
 43 
 44     int tg; scanf("%d", &tg);
 45     int dd, ff;
 46     int i, j;
 47 
 48     while(tg--){
 49         scanf("%d %d", &dd, &ff);
 50         if(dd==ff){
 51             printf("0
"); continue;
 52         }
 53         node s, e;
 54         s.a=dd/1000; s.b=dd/100%10; s.c=dd/10%10; s.d=dd%10; s.path=0;
 55         e.a=ff/1000; e.b=ff/100%10; e.c=ff/10%10; e.d=ff%10;
 56 
 57         queue<node>q; bool flag=false;
 58         q.push(s); node cur;
 59         int ans=0;
 60         memset(vis, false, sizeof(vis));
 61         vis[dd]=true;
 62 
 63         while(!q.empty()){
 64             cur=q.front(); q.pop();
 65 
 66             for(i=1; i<=9; i+=2){//枚举个位 偶数排除
 67                 int temp=cur.a*1000+cur.b*100+cur.c*10+i;
 68                 if(!vis[temp] && f[temp]==0){
 69                     node cc=cur; cc.d=i; cc.path=cur.path+1;
 70                     vis[temp]=true;
 71                     if(temp==ff){
 72                     flag=true; ans=cc.path; break;
 73                     }
 74                     q.push(cc);
 75                 }
 76             }
 77             if(flag) break;
 78 
 79             for(i=0; i<=9; i++){ //枚举个位
 80 
 81                 int temp=cur.a*1000+cur.b*100+i*10+cur.d;
 82                 if(!vis[temp] &&f[temp]==0){
 83                     node cc=cur; cc.c=i; cc.path=cur.path+1;
 84                     vis[temp]=true;
 85                     if(temp==ff){
 86                         flag=true; ans=cc.path; break;
 87                     }
 88                     q.push(cc);
 89                 }
 90             }
 91             if(flag) break;
 92 
 93             for(i=0; i<=9; i++){//枚举百位
 94                 int temp=cur.a*1000+i*100+cur.c*10+cur.d;
 95                 if(!vis[temp] &&f[temp]==0){
 96                     node cc=cur; cc.b=i; cc.path=cur.path+1;
 97                     vis[temp]=true;
 98                     if(temp==ff){
 99                         flag=true; ans=cc.path; break;
100                     }
101                     q.push(cc);
102                 }
103             }
104             if(flag) break;
105 
106             for(i=1; i<=9; i++){ //枚举千位 千位不能为0
107                 int temp=i*1000+cur.b*100+cur.c*10+cur.d;
108                 if(!vis[temp] &&f[temp]==0){
109                     node cc=cur; cc.a=i; cc.path=cur.path+1;
110                     vis[temp]=true;
111                     if(temp==ff){
112                         flag=true; ans=cc.path; break;
113                     }
114                     q.push(cc);
115                 }
116             }
117             if(flag) break;
118         }
119         if(flag) printf("%d
", ans );
120         else printf("Impossible
");
121     }
122     return 0;
123 }
View Code

原文地址:https://www.cnblogs.com/yspworld/p/4699643.html