zoukankan      html  css  js  c++  java
  • 素数路径Prime Path POJ3126 素数,BFS

    题目链接:Prime Path

    题目大意

    从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数。求最终把m变成n所需的最少次数。

    思路

    BFS。搜索的时候,最低位为0,2,4,6,8可以先排除(偶数不会是素数),最高位是0的也可以排除。这个题判断素数的次数比较少,可以不打素数表。

    题解

    第二次写的时候代码写的很乱。。没有第一遍干净了

      1 #include <iostream>
      2 #include <cstring>
      3 #include <queue>
      4 using namespace std;
      5 
      6 int num, m, n, ans;
      7 int vis[10005];
      8 int a[10] = {0,1,2,3,4,5,6,7,8,9};
      9 int digit[5];
     10 struct point
     11 {
     12     int val;
     13     int step;
     14 }p;
     15 
     16 void div(int x)        //把数拆分
     17 {
     18     for(int i = 0; i < 4; i++)
     19     {
     20         digit[i] = x % 10;
     21         x = x/10;
     22     }
     23 }
     24 
     25 
     26 bool isPrime(int n)        
     27 {
     28     for(int i = 2; i*i <= n; i++)
     29     {
     30         if(n % i == 0)
     31         {
     32             return false;
     33         }
     34     }
     35     return true;
     36 }
     37 
     38 
     39 int bfs(int m, int n)
     40 {
     41     queue<point> q;
     42     p.val = m;
     43     p.step = 0;
     44     q.push(p);
     45     while(!q.empty())
     46     {
     47         point cur = q.front();
     48         if(cur.val == n)
     49         {
     50             return cur.step;
     51         }
     52         //cout << cur.val << " ";
     53         vis[cur.val] = 1;
     54         div(cur.val);
     55         for(int i = 0; i < 10; i++)
     56         {
     57             if(i == digit[0] || i == 2 || i == 4 || i == 6 || i == 8) continue;
     58             int x = digit[3]*1000 + digit[2]*100 + digit[1]*10 + i;
     59             if(vis[x] || !isPrime(x)) continue;
     60             p.val = x;
     61             p.step = cur.step+1;
     62             q.push(p);
     63         }
     64         for(int i = 0; i < 10; i++)
     65         {
     66             if(i == digit[1]) continue;
     67             int x = digit[3]*1000 + digit[2]*100 + i*10 + digit[0];
     68             if(vis[x] || !isPrime(x)) continue;
     69             p.val = x;
     70             p.step = cur.step+1;
     71             q.push(p);
     72         }
     73 
     74         for(int i = 0; i < 10; i++)
     75         {
     76             if(i == digit[2]) continue;
     77             int x = digit[3]*1000 + i*100 + digit[1]*10 + digit[0];
     78             if(vis[x] || !isPrime(x)) continue;
     79             p.val = x;
     80             p.step = cur.step+1;
     81             q.push(p);
     82         }
     83         for(int i = 1; i < 10; i++)
     84         {
     85             if(i == digit[3]) continue;
     86             int x = i*1000 + digit[2]*100 + digit[1]*10 + digit[0];
     87             if(vis[x]|| !isPrime(x)) continue;
     88             p.val = x;
     89             p.step = cur.step+1;
     90             q.push(p);
     91         }
     92         q.pop();
     93     }
     94     return -1;            //没有找到
     95 }
     96 
     97 
     98 int main(int argc, char const *argv[])
     99 {
    100     #ifdef debug
    101         freopen("test.txt","r",stdin);
    102     #endif
    103     cin >> num;
    104     while(num--)
    105     {
    106         memset(vis, 0 ,sizeof(vis));
    107         cin >> m >> n;
    108         ans = bfs(m, n);
    109         if (ans == -1)
    110         {
    111             cout << "Impossible" << endl;
    112             continue;
    113         }
    114         cout << ans << endl;
    115     }
    116 }
  • 相关阅读:
    建造者模式
    js日期转化(计算一周的日期)
    vue实现全选效果
    less入门
    使用node初始化项目
    ES5新语法forEach和map及封装原理
    es6中的promise对象
    深入理解jsonp跨域请求原理
    markdown语法与使用
    Ajax商品分类三级联动实现
  • 原文地址:https://www.cnblogs.com/SaltyFishQF/p/10299645.html
Copyright © 2011-2022 走看看