zoukankan      html  css  js  c++  java
  • 个位数字poj 3126 Prime Path

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!

        题意:从一个四位素数变到另一个四位素数,每次只能改到一个位的数字,且修改后仍是一个四位素数,问最少须要几步。

        题目链接:http://poj.org/problem?id=3126

        ——>>直接广搜。

        每日一道理
    父亲对于儿子来说,是座耸立的高山,而儿子只是颗石子,源于山,却并不了解山。生活中诸多爱的密码,是需用细节来解读的,在亲情的沃土上,要想搞得最美的果实,惟有期待那存在于瞬间的心与心的共鸣,爱与爱的默契。
    #include <cstdio>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    const int maxn = 9999 + 10;
    int MAP[maxn], a, b, p[maxn], cnt;
    bool vis[maxn];
    
    int translate(int x, int i, int j)      //把x的第i位变成j
    {
        int k, buf[4], ret = 0;
        for(k = 0; k < 4; k++)
        {
            buf[k] = x % 10;
            x /= 10;
        }
        buf[i] = j;
        for(k = 3; k >= 0; k--) ret = ret * 10 + buf[k];
        return ret;
    }
    
    bool bs(int* A, int x, int y, int v)        //二分搜索
    {
        int m;
        while(x < y)
        {
            m = x + (y - x) / 2;
            if(A[m] == v) return 1;
            else if(A[m] > v) y = m;
            else x = m + 1;
        }
        return 0;
    }
    
    void get_prime()        //倏地筛素数
    {
        memset(vis, 0, sizeof(vis));
        cnt = 0;
        for(int i = 2; i <= 9999; i++) if(!vis[i])
        {
            p[cnt++] = i;
            for(int j = i; j <= 9999; j = j + i) vis[j] = 1;
        }
    }
    
    void bfs()
    {
        if(a == b) return;
        int temp, i, j;
        queue<int> qu;
        qu.push(a);
        while(!qu.empty())
        {
            temp = qu.front();
            qu.pop();
            for(i = 0; i < 4; i++)
                for(j = 0; j < 10; j++)
                {
                    if(j == 0 && (i == 0 || i == 3)) continue;
                    int tmp = translate(temp, i, j);
                    if(tmp % 2 == 0 || MAP[tmp] != -1) continue;
                    if(bs(p, 168, cnt, tmp))
                    {
                        MAP[tmp] = MAP[temp] + 1;
                        if(tmp == b) return;
                        qu.push(tmp);
                    }
                }
        }
    }
    
    int main()
    {
        int T;
        get_prime();
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d%d", &a, &b);
            memset(MAP, -1, sizeof(MAP));
            MAP[a] = 0;
            bfs();
            printf("%d\n", MAP[b]);
        }
        return 0;
    }

    文章结束给大家分享下程序员的一些笑话语录: 联想——对内高价,补贴对外倾销的伟大“民族”企业。

    --------------------------------- 原创文章 By
    个位和数字
    ---------------------------------

  • 相关阅读:
    js:delete 操作符
    ActiveReports 代码控制报表连续打印
    ActiveReports 打印条码无法设置纸张大小?
    《JavaScript高级程序设计》读书笔记之一:几个与原始类型等价的引用类型的常用方法和属性
    js:字符串类型快速转化成数字类型和数字类型快速转化为字符串类型
    js:for...in 语句(JavaScript For...In Statement)
    Internet Explorer Developer Toolbar
    javascript 定义对象的方法
    关于 输入框输入一定数以后跳转的问题
    javascript 关于弹出关闭的例子
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3104970.html
Copyright © 2011-2022 走看看