素数通道

  题目大意:给定两个素数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. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  2. django book

    一.安装配置 1.下载地址: https://www.djangoproject.com/download/ 2.安装: tar zxvf Django-1.6.1.tar.gz && ...

  3. H5移动端知识点总结

    H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解display:box的布局 理解flex布局 Flex布局兼容知识点总结 回到顶部 移动开 ...

  4. SQL按指定文字顺序进行排序(中文或数字等)

    在有些情况下我们需要按指定顺序输出数据,比如选择了ID in(3,1,2,5,4)我们希望按这个3,1,2,5,4的顺序输出,这样只使用order by ID是无法实现的, 但是我们可以使用order ...

  5. Code First02---CodeFirst配置实体与数据库映射的两种方式

    Code First有两种配置数据库映射的方式,一种是使用数据属性DataAnnotation,另一种是Fluent API. 这两种方式分别是什么呢?下面进行一一解释: DataAnnotation ...

  6. JQuery选择器细节-遁地龙卷风

    1.层次选择器-子元素选择器 <body> <div> <p>lol</p> <div> <p></p> </ ...

  7. css固定元素位置(fixed)

    来源:http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html fixed是一种特殊的absolute,同样不占文档流,特殊的地方在于fix ...

  8. Unity手游之路<九>自动寻路Navmesh之高级主题

    http://blog.csdn.net/janeky/article/details/17492531 之前我们一起学习了如何使用Navmesh组件来实现最基本的角色自动寻路.今天我们再继续深入探索 ...

  9. 14个Xcode中常用的快捷键操作(转)

    2014-12-24 17:31 编辑: suiling 分类:iOS开发 来源:iPhoneDev.tv 13 31621 Xcode 6Xcode快捷键 招聘信息: 开发工程师 iOS开发工程师 ...

  10. union联合体

    今天笔试的一道题,好久没用union了,竟然忘光光了. 关于其大小的计算,分两步:先算对齐大小(成员中字节最大的那个),再算分配空间: 不仅是对齐大小的整数倍,还要满足实际大小不能小于最大成员大小. ...