Prime Path(POJ - 3126)【BFS+筛素数】】的更多相关文章

Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然为质数. 2.四位数的范围是1000~9999,之间共有1000多个质数.由于已经知道位数为4位,所以可以通过BFS来寻找最小步数.每次需要分别变换个位.十位.百位.千位,并且把符合要求的数放到队列中,同时需标记这个数已经遍历过一次,避免重复遍历,直到找到目标数. C++代码 #include<io…
题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次数,否则输出“Impossible”.(N,M必须一直是素数) 思路:bfs.四位数,每一位可以替换为0~9,那么我们可以每次改变N中的一位数,然后放入队列中,当然,在替换数字时难免会出现重复的四位数,这样会造成TLE,那么我们可以创建一个bool数组标记出现过的,我们也需要素数筛999 ~ 100…
素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的最小步数就可以了,每个权都是1,所以用BFS就可以了,一开始我还用DFS,太丢人了,一开始就把素数表打好就可以了 #include <iostream> #include <functional> #include <algorithm> using namespace s…
Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 6640 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to c…
Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig…
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.— It is a matter of security to change such things every now and then,…
Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th…
Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14539   Accepted: 8196 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig…
描述 孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了.zydsg要做些改变,他想去和素数小姐姐约会. 所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样,如果两个路口间只差1个数字,则有一条路连通两个路口.(例如1033和1073间有一条路连接) 现在,你知道了zydsg的位置和素数小姐姐的家,问最少zydsg要走多少条路才能见到素数小姐姐.例如:如果zydsg在1033,素数小姐姐的家在8179,最少要走6条街,走法为: 1033 1733 37…
题意:给定两个四位素数作为终点和起点,每次可以改变起点数的某一位,且改变后的数仍然是素数,问是否可能变换成终点数字? 思路:bfs搜索,每次改变四位数中的某一位.素数打表方便判断新生成的数是否是素数. AC代码 #include<cstdio> #include<cstring> #include<queue> #include<cmath> using namespace std; const int maxn = 1e5 + 5; int vis[max…