zoukankan      html  css  js  c++  java
  • 【POJ3126】Prime Path

    本题传送门

    本题知识点:宽度优先搜索

    题意很简单。要找一个质数变到另一个质数的最少步数,两个质数都是4位数,变的时候只能变其中一位,变了的数也仍是质数。

    思路也很简单,对每一位数进行修改,如果修改后的数仍是质数则入队。

    要注意的是千位数不能是0。

    数据很小。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    int T;
    int l, r;
    bool take[100005];
    struct node{
        int num;
        int t;
    };
    queue<node> que;
    bool ok;
    
    bool check(int n){
        for(int i = 2; i * i <= n; i++){
            if(n % i == 0) return false;
        }
        return true;
    }
    
    void bfs(){
        node a;
        a.num = l; a.t = 0;
        que.push(a);
        take[l] = true;
    
        while(!que.empty()){
            node next, now = que.front(); que.pop();
            int f;
    //        printf("now num:%d t:%d   r:%d
    ", now.num, now.t, r);
            if(now.num == r){
                printf("%d
    ", now.t);
                ok = true;
                break;
            }
    
            // 个位
            f = now.num;
            f = f / 10 * 10;
            for(int i = 0; i <= 9; i++){
                int g = f + i;
                if(check(g) && !take[g]){
                    next.num = g; next.t = now.t + 1;
                    take[g] = true;
                    que.push(next);
                }
            }
    
            // 十位
            f = now.num;
            f = f / 100 * 100 + f % 10;
            for(int i = 0; i <= 9; i++){
                int g = f + i * 10;
                if(check(g) && !take[g]){
                    next.num = g; next.t = now.t + 1;
                    take[g] = true;
                    que.push(next);
                }
            }
    
            // 百位
            f = now.num;
            f = f / 1000 * 1000 + f % 100;
            for(int i = 0; i <= 9; i++){
                int g = f + i * 100;
                if(check(g) && !take[g]){
                    next.num = g; next.t = now.t + 1;
                    take[g] = true;
                    que.push(next);
                }
            }
    
            // 千位
            f = now.num;
            f = f % 1000;
            for(int i = 1; i <= 9; i++){
                int g = f + i * 1000;
                if(check(g) && !take[g]){
                    next.num = g; next.t = now.t + 1;
                    take[g] = true;
                    que.push(next);
                }
            }
        }
    }
    
    int main()
    {
        scanf("%d", &T);
        while(T--){
            memset(take, false, sizeof(take));
            scanf("%d %d", &l, &r);
            while(!que.empty()) que.pop();
            ok = false;
            bfs();
            if(!ok) printf("Impossible
    ");
        }
        return 0;
    }
    
    
  • 相关阅读:
    HFun.快速开发平台(一)=》简述
    技术分工论批判(要义)
    后产品意识形态
    鼠标悬停显示图片
    那些特殊边框效果在报表中要怎样实现?
    润乾报表美化设置 -- 样式
    润乾报表如何从 mongodb 中取数
    数据采集录入填报时如何只更新当前修改行
    数据采集填报中自动计算的指标如何做
    数据报表多种序号生成方式
  • 原文地址:https://www.cnblogs.com/Ayanowww/p/11561576.html
Copyright © 2011-2022 走看看