素数通道

  题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible

  水题,因为要求最小步数,所以我们只需要找到到每个素数的最小步数就可以了,每个权都是1,所以用BFS就可以了,一开始我还用DFS,太丢人了,一开始就把素数表打好就可以了

  

 #include <iostream>
#include <functional>
#include <algorithm> using namespace std; typedef int Position;
typedef struct _set
{
char num[];
}Set,Queue;
bool BFS(const int);
void Search_Prime(void);
int Get_Num(char *); static bool prime_set[];
static char num[], goal_set[];
static int min_step[], pow_sum[] = { , , , };
static void Push(char *,Position);
static char *Pop(Position);
Queue que[ * ]; int main(void)
{
int case_sum, goal_num; Search_Prime();
scanf("%d", &case_sum); while (case_sum--)
{
getchar();
scanf("%c%c%c%c", &num[], &num[], &num[], &num[]);
getchar();
scanf("%c%c%c%c", &goal_set[], &goal_set[], &goal_set[], &goal_set[]);
goal_num = Get_Num(goal_set);
if (!BFS(goal_num))
printf("Impossible\n");
} return ;
} int Get_Num(char *s)
{
int ans = ;
for (int i = ; i < ; i++)
ans += (s[ - i] - '') * pow_sum[i];
return ans;
} void Search_Prime(void)//线筛法打表
{
int i, j;
prime_set[] = ;
memset(prime_set, , sizeof(prime_set));
for (i = ; i < ; i++)
{
for (j = ; j <= i && i*j < ; j++)
if (!prime_set[j])
prime_set[i*j] = ;
}
} static void Push(char *num, Position back)
{
for (int i = ; i < ; i++)
que[back].num[i] = num[i];
} static char *Pop(Position head)
{
return que[head].num;
} bool BFS(const int goal)
{
memset(min_step, -, sizeof(min_step));
Position head = , back = ;
int i, j, sum_tmp, step_now;
char *out, tmp_int; sum_tmp = Get_Num(num);
if (sum_tmp == goal)
{
printf("0\n");
return true;
}
Push(num, back++); min_step[sum_tmp] = ; while (head != back)
{
out = Pop(head++); sum_tmp = Get_Num(out);
step_now = min_step[sum_tmp];
for (i = ; i < ; i++)
{
tmp_int = out[i];
for (j = ; j < ; j++)
{
if (i == && j == || tmp_int == j + '') continue;
out[i] = j + '';
sum_tmp = Get_Num(out);
if (min_step[sum_tmp] == - && !prime_set[sum_tmp])
{
min_step[sum_tmp] = step_now + ;
Push(out, back++);
if (sum_tmp == goal)
{
printf("%d\n", min_step[sum_tmp]);
return true;
}
}
}
out[i] = tmp_int;
}
}
return false;
}

Mathematics:Prime Path(POJ 3126)的更多相关文章

  1. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  2. kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126

    题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...

  3. POJ - 3126 - Prime Path(BFS)

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

  4. 双向广搜 POJ 3126 Prime Path

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

  5. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  7. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  8. Prime Path(poj 3126)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  9. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

随机推荐

  1. centos 安装php7.0.2

    PHP7.0正式版已经在2015年11月份左右发布,目前是PHP7.0.2版本,本人最早是从2015年8月php7的第一个测试版跟起,现在正式版发布. linux版本:64位CentOS 6.6 Ng ...

  2. 让我们一起学Node.js-文章列表

    新浪的博客最近不给力,只好在博客园落个窝.至此之后,技术随笔会在博客园以及新浪的博客上同时更新,如果新浪给力的话~~~ 如果你想看先前新浪博客上分享的技术,请点击此处 忘尘子新浪博客! 我是拜读了朴灵 ...

  3. 2015年12月01日 GitHub入门学习(一)GitHub简介

    序:Github理念是Social Coding(社会化编程).octocat是它的吉祥物. 一.Github与Git的区别与联系 区别:GIT是仓库,Github是提供一种将代码提交到Git仓库的服 ...

  4. R语言练习(二)

    op <- par(mfrow = c(2, 2)) #设置画布 p2 <- curve(x^2, 0, 1) #绘制曲线 legend("topleft", inse ...

  5. compareTo(String str)与compareToIgnoreCase(String str)

    一.compareTo(String str)方法 返回值:如果参数字符串等于此字符串,则返回值 0:如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值:如果此字符串按字典顺序大于字符串参 ...

  6. EF接触02

    Ado.net Entity Framework早期称为ObjectSpace.基于Ado.net操作数据库的一组类库. 什么是ADO.NET? 基础.net平台下的操作数据库的一组Api或组建.五大 ...

  7. [HDU3555]Bomb

    [HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...

  8. JDIC 访问Web时NullPointerException

    Exception in thread "EventThread" java.lang.NullPointerException            at org.jdeskto ...

  9. 教你搭建SpringSecurity3框架( 更新中、附源码)

    源码下载地址:http://pan.baidu.com/s/1qWsgIg0 一.web.xml <?xml version="1.0" encoding="UTF ...

  10. 二分图水一波~~~~d带你飞

    Current Time: 2016-03-11 17:45:36 Contest Type: Public Start Time: 2016-03-04 13:00:00 Contest Statu ...