题目大意:
素数路径
估计看数据就明白这道题什么意思了......给两个素数,都是四位数的素数,并且没有前导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. 对xml操作

    已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...

  2. How to Make LastPass Even More Secure with Google Authenticator

    Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...

  3. Java反射学习(java reflect)(二)

    ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...

  4. Linux ./configure && make && make install 编译安装和卸载

    正常的编译安装/卸载: 源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install).   configure文件是一个可执行的脚本文件,它有很多选项, ...

  5. 关于css float 属性以及position:absolute 的区别。

    1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...

  6. illegal mix of collcations表连接时非法的校对

    背景:旧表导入新表,新表里的字段是字符串类型 新表是int类型 两个字段通过字符串处理后相等 (准备left join 关联起来)报错 把int类型字段更改成varchar字符串类型后成功

  7. Unix/Linux 'dirctory tree' command.

    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...

  8. emmet插件的导入与实用

    http://jingyan.baidu.com/article/ff4116259b057c12e48237b8.html http://www.iteye.com/news/27580 分享htm ...

  9. iOS题

    对于语句NSString* testObject = [[NSData alloc] init];关于testObject是什么类型对象,以下说法正确的是: 答案:(A) A.编译时,NSString ...

  10. 常用 Linux 命令

    Check page size: getconf PAGESIZE Check memory information: cat /proc/meminfo Check number of hugepa ...