题目大意:
素数路径
估计看数据就明白这道题什么意思了......给两个素数,都是四位数的素数,并且没有前导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. .net 使用AjaxControlToolkit.dll 遇到的"Sys"未定义问题

    1.配置文件一般都会缺少<httpHandlers></httpHandlers> 这一段, <httpHandlers> <remove verb=&quo ...

  2. 禁用windows 10自动更新

    按Win键+R键调出运行,输入“gpedit.msc”点击“确定”,调出“本地组策略编辑器”.顺序依次展开计算机配置,管理模板 ,windows组件 ,windows更新 点击右边“配置自动更新”,选 ...

  3. 绘图quartz之渐变

    实现线性渐变   径向渐变   自己新建的view中有一个drawRect:(cgrect)rect方法 在这个方法里 可以通过画图 将内容显示在画板上(即最下边的view)   渐变的方式分两种   ...

  4. Linux 添加epel源

    1.epel-release yum install epel-release 这样有些没办法通过yum 安装  可以这样安装(例如redis)

  5. js正则验证方法大全

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. Java设计模式(学习整理)---工厂模式

    1.工厂模式 1.1 为什么使用工厂模式? 因为工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时 ...

  7. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  8. 防止iframe嵌套

    如果你哪个页面不想被嵌套 下面js代码可以解决(我的是火狐) 慎用 <script type="text/javascript">          window.on ...

  9. shell调用shell

    在默认条件下,执行shell文件会出现permission denied报错,一般是没有可执行权限.用chmod修改权限 chomd 777 score.sh   //把所有权限给aa文件 777代表 ...

  10. innerHtml写法

    swt_center = "<div id='new_swt_wee'>"; swt_center += '<a href="javascript:vo ...