• poj 3126 Prime Path


      题目大意就是输入两个四位数 a,b; 你要以a为起点,每次可以改变四位中任意一位,且每次改变后得到的数要仍然为素数。 问最少要多少步才能变到b,否则输出Impossible。

      每次变换一位,求最少步;所以应该不难得出这是一道典型的bfs题。      先生成素数表1-10000,方便后面判断是否为素数;再开一个标记数组book用来标记出现过的数字。  然后对于输入的a转换为string进行bfs,每次生成新的数字都对其判断是否为素数及是否已出现过。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <cstdlib>
     7 #include <cctype>
     8 #include <queue>
     9 #include <stack>
    10 #include <map>
    11 #include <vector>
    12 #include <set>
    13 #include <utility>
    14 #define ll long long
    15 #define inf 0x3f3f3f3f
    16 using namespace std;
    17 
    18 bool prime[10000];
    19 void get_prime() //生成素数表
    20 {
    21     fill(prime,prime+10000,true);
    22     prime[1]=false;
    23     for(int i=2; i<=sqrt(10000); i++)
    24         if(prime[i])
    25             for(int j=i+i; j<10000; j+=i)
    26                 prime[j]=false;
    27 }
    28 int convert(string str)  //将字符串转为数值
    29 {
    30     return (str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');
    31 }
    32 typedef pair<string,int> P;
    33 string l,r;
    34 void solve()
    35 {           
    36     map<string,int> book;  //标记数组
    37     queue<P> q;
    38     book[l]=1;
    39     q.push(P(l,0));
    40     //   BFS
    41     while(!q.empty())
    42     {
    43         P temp=q.front();
    44         q.pop();
    45         for(int i=0; i<4; i++)
    46         {
    47             string str=temp.first;
    48             for(int j=0; j<=9; j++)
    49             {
    50                 if(!i&&!j) continue;
    51                 //
    52                 str[i]='0'+j;
    53                 if(str==r)
    54                 {
    55                     printf("%d
    ",temp.second+1);
    56                     return;
    57                 }
    58                 //
    59                 if(prime[convert(str)])
    60                     if(!book[str])
    61                     {
    62                         book[str]=1;
    63                         P tt;
    64                         tt.first=str,tt.second=temp.second+1;
    65                         q.push(tt);
    66                     }
    67             }
    68         }
    69     }
    70     printf("Impossible
    ");
    71 }
    72 int main()
    73 {
    74     //freopen("input.txt","r",stdin);
    75     get_prime();
    76     int n;
    77     scanf("%d",&n);
    78     while(n--)
    79     {
    80         cin>>l>>r;
    81         if(l==r)
    82             printf("0
    ");
    83         else
    84             solve();
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    Ubuntu20.04更换软件源
    使用kubeadm安装k8s1.19版本之系统基础环境配置&k8s集群初始化(二)
    k8s如何删除处于terminating状态的ns资源
    k8s如何强制删除pod&pv&pvc和ns&namespace方法
    C语言中的有符号数和无符号整形数转换
    互联网-架构演进
    结合redis使token失效
    有一种爱叫做放手
    js 读取上传的json文件内容
    使用spark-md5获取文件的MD5值
  • 原文地址:https://www.cnblogs.com/geek1116/p/5673351.html
Copyright © 2020-2023  润新知