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

    use Test --进入需要建表的数据库if exists(select * from sysobjects where name='MyTable')--表是否已经存在drop table MyT ...

  2. linux创建用户

    创建用户   sudo adduser xxx 删除用户   sudo userdel xxx 删除用户和目录  sudo userdel -r xxx

  3. 新装的mysql,直接安装板

    Windows安装MySQL解压版 http://www.cnblogs.com/xiaoit/p/3932241.html my文件 如下: [mysql]# 设置mysql客户端默认字符集defa ...

  4. IE浏览器设置

  5. iOS GCD多线程介绍

    GCD:是纯C语言写的,是苹果公司为多核的并行运算提出的解决方案. GCD的两个核心概念: - 任务 - 队列 将任务添加到队列中 GCD会自动将队列中的任务取出,放到对应的线程中执行 任务的取出遵循 ...

  6. linux 定时执行shell

    第一步:安装 crontab ,命令 yum -y install vixie-cron                扩展:service crond start //启动服务            ...

  7. wdcp-apache开启KeepAlive提高响应速度

    因为我们的网站,媒体文件,js文件,css文件等都在同一个服务器上,并且,我们网站有非常多的图片,所以当建立好tcp链接之后,不应该马上关闭连接,因为每建立一次连接还要进行dns解析,以及启动一个ht ...

  8. QTP的DataTable操作整理(注---不知转载多少遍)

    返回值:数字 示例: 以下示例使用 GetRowCount 方法查找 MySheet 运行时数据表中最长的列中的总行数,并将其写入报告. rowcount = DataTable.GetSheet(& ...

  9. linux安装时提示发生不正常错误问题

    跳过md5系统较检(每个系统版本都有一个md5编码唯一) 在安装CentOS时 提示 找不到磁盘,是否安装程序,选择安装程序进行"下一步" 提示: 发生不规则,不正常错误 原因:没 ...

  10. centos下安装chdmg

    http://www.aboutyun.com/thread-9075-1-1.html  基本参考这个   yum clean all yum update     1.保证selinux关闭  / ...