• uestc 851 方老师与素数


    看了大牛的代码

    新技能get√

    如何优雅的计算广搜的次数

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<iostream>
     6 #include<queue>
     7 #include<string>
     8 #include<cmath>
     9 using namespace std;
    10 #define MAXN 10001
    11 
    12 bool prime[MAXN];
    13 int vis[MAXN];
    14 int a, b, t;
    15 
    16 void deal()
    17 {
    18     for (int i = 2; i <= MAXN; ++i)
    19         if (!prime[i])
    20             for (int j = i*i; j <= MAXN; j += i)
    21                 prime[j] = true;
    22     /*for (int i = 0; i < 50; ++i)
    23         if (!prime[i]) cout << i << " ";
    24     cout << endl;*/
    25 }
    26 
    27 int bfs()
    28 {
    29     queue<int> q;
    30     q.push(a);
    31     q.push(-1);
    32     int ans = 0;
    33     while (!q.empty()) {
    34         int t = q.front();
    35         if (t == -1) {
    36             q.pop();
    37             if (q.empty()) return -1;
    38             ans++;
    39             q.push(t); continue;
    40         }q.pop();
    41         if (t == b) return ans;
    42         int s;
    43         for (int i = 1; i <= 9; ++i) {
    44             s = i * 1000 + t % 1000;
    45             if (!vis[s] && !prime[s]) {
    46                 q.push(s);
    47                 vis[s] = true;
    48             }
    49         }
    50         for (int i = 0; i <= 9; ++i) {
    51             s = i * 100 + t % 100 + t/1000*1000;
    52             if (!vis[s] && !prime[s]) {
    53                 q.push(s);
    54                 vis[s] = true;
    55             }
    56         }
    57         for (int i = 0; i <= 9; ++i) {
    58             s = i * 10 + t % 10 + t/100*100;
    59             if (!vis[s] && !prime[s]) {
    60                 q.push(s);
    61                 vis[s] = true;
    62             }
    63         }
    64         for (int i = 0; i <= 9; ++i) {
    65             s = i + t / 10 * 10;
    66             if (!vis[s] && !prime[s]) {
    67                 q.push(s);
    68                 vis[s] = true;
    69             }
    70         }
    71     }
    72     return -1;
    73 }
    74 
    75 int main()
    76 {
    77     int  t;
    78     cin >> t;
    79     deal();
    80     while (t--) {
    81         cin >> a >> b;
    82         memset(vis,0,sizeof(vis));
    83         int res = bfs();
    84         if (res == -1) puts("Impossible");
    85         else cout << res << endl; 
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    娿
    我不知道啊
    Android怎么把引入的library库工程转换成jar包
    高斯消元入门和简单应用
    数论函数基本知识
    AC自动机入门和简单应用
    FFT和NTT
    同余系基本知识
    虚树学习笔记
    Windows常用快捷键和基本的Dos命令
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4260173.html
Copyright © 2020-2023  润新知