题目大意:
素数路径
估计看数据就明白这道题什么意思了......给两个素数,都是四位数的素数,并且没有前导0,现在需要经过一种变换把一个素数转换成另一个,当然这种转换是有规则的,规则就是每次只能改变这个四位数的其中一位数字,当然改变后的数字也得是素数,问最少的改变次数是多少......
貌似还是广搜..............................................................................................不过做起来应该会麻烦点,要求素数,不过可以搞一个素数表这样判断起来更方便
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std; #define maxn 10000 int p[maxn];//标记4位数的素数 int Prime(int n)
{
    int i, k=sqrt(n);     for(i=; i<=k; i++)
        if(n % i == )
            return ;     return ;
}
int Turn(int n, int k)//把n的第k位转换成0
{
    char s[]={};     sprintf(s, "%d", n);
    s[k] = '';
    sscanf(s, "%d", &n);     return n;
}
int BFS(int s, int e)
{
    int i, j, k, q;
    int v[maxn]={};
    queue<int> Q;     Q.push(s);
    v[s] = ;     while(Q.size())
    {
        s = Q.front();Q.pop();         if(s == e)
            return v[s]-;         int t = ;         for(i=; i<; i++)
        {
            q = Turn(s, i);             for(k=; k<; k++)
            {
                j = q+k*t;                 if(p[j] ==  && v[j] == )
                {
                    Q.push(j);
                    v[j] = v[s] + ;
                }
            }             t /= ;
        }
    }     return -;
} int main()
{
    int i, s, e, T;     for(i=; i<maxn; i++)
        p[i] = Prime(i);     scanf("%d", &T);     while(T--)
    {
        scanf("%d%d", &s, &e);         int ans = BFS(s, e);         if(ans == -)
            printf("Impossible\n");
        else
            printf("%d\n", ans);
    }     return ;

}

F - Prime Path的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 - F - Prime Path

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  2. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  3. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  4. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  5. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  8. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  9. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

随机推荐

  1. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  2. html 中 #include file 的用法

    有两个文件a.htm和b.htm,在同一目录下a.htm内容如下 <!-- #include file="b.htm" --> b.htm内容如下 今天:雨 31 ℃- ...

  3. js 中的流程控制-条件语句

    条件语句: if(exp)执行一句代码 <script> var x = 1 ; if(x == 1 ) //当if判断语句结果是true 或者 false 当判断结果等于true的时候, ...

  4. 隐藏和显示 ng-show ng-hide

    <div ng-controller='DeathraymenueController'>    <button ng-click="toggleMenue()" ...

  5. Python 基础-python函数

    函数    1.def    2.命名    3.函数体    4.return 返回值 def get_return(): a = 1 return a 函数参数有 形参和实参    定义几个形参就 ...

  6. 七天学会SALTSTACK自动化运维 (2)

    七天学会SALTSTACK自动化运维 (2) 导读 Grains Pillar 总结 参考链接 导读 上一篇主要介绍了安装和基本的使用方法,但是我认为如果理解了相关概念的话,使用会更加顺手,因为毕竟每 ...

  7. Python Tutorial 学习(十)-- Brief Tour of the Standard Library

    10.1. Operating System Interface os库 import os os.getcwd() # Return the current working directory 'C ...

  8. 尽量使用ToUpper比较,避免使用ToLower

    在编码时尽量使用ToUpper比较,避免使用ToLower,因为微软对ToUpper进行了优化,以下为测试结果: public void TestToLower() { Stopwatch watch ...

  9. 串行CPU设计

    一.概述 串行CPU工作流程 串行CPU的时序流程如下图所示:取指.译码.执行.回写. 其中,取指.回写是与存储器打交道:而译码与执行则是CPU内部自个儿的操作. 我们究竟想要CPU干什么?     ...

  10. ural1057Amount of Degrees

    Description Create a code to determine the amount of integers, lying in the set [ X; Y] and being a ...