Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 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-digi…
方老师与素数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方老师最近很喜欢素数,他想玩一个游戏: 现在有两个44位的素数nn和mm,你一次可以改变nn的一位数字,并且改变过后的新数字必须也是个素数,并且也不能有前导00.请问使nn变为mm最少需要多少步. 例如n=1033n=1033 m=8179m=8179 那么可行的变化是: 1033 173…
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32036   Accepted: 17373 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they…
意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素数经过一次改变而来的素数  知道得到b #include <cstdio> #include <cstring> using namespace std; const int N = 10000; int p[N], v[N], d[N], q[N], a, b; void initP…
[POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: 18966 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…
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来搜了一下解题报告,才发现是bfs(). 想想也是,题目其实已经提示的很清楚了,求最短的路径,对于每一个数,每次可以把4位中的任意一位,  换成与该位不相同的0-9中的任意一位,对于迷宫类 bfs每次转移数为上下左右四个状态,而此题就相当于每次可以转移40个状态(其实最低位为偶数可以排除,不过题目数据…
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素数: #include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<…
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大... /************************************************************************* > File Name: E.cpp > Author: > Mail: > Created Time: 2017年11月26日…
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复…
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数是以前被换下的). 思路:若素数a可以按上述规则转化为b,则可以看做a.b直接有一条边.显然,从初始值到目标值的路径上的边数即为花费的 数目,这样一来,就相当于求最短路径.由于题目只要求最小花费数,所以不需要存储有向图. 用BFS搜索,每次枚举当前值x所能变换得到的值y,若y满足条件,将y以及从初始…
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<map> #include<queue> #include<stdlib.h> #include<string&g…
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个改变的数之前已经得到过了, 就没有必要继续变回它了. 代码例如以下: #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue…
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K 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-digit room numbers on…
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然为质数. 2.四位数的范围是1000~9999,之间共有1000多个质数.由于已经知道位数为4位,所以可以通过BFS来寻找最小步数.每次需要分别变换个位.十位.百位.千位,并且把符合要求的数放到队列中,同时需标记这个数已经遍历过一次,避免重复遍历,直到找到目标数. C++代码 #include<io…
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 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-digit…
Prime Path 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 a…
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-digit room numbers on their offices. — It is a matter of security to change such things every now…
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, to…
  POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 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…
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数的最小值.(a,b,c都是四位数字,输入时没有前导零) 分析: 每次改变可以获得一个四位数c,然后如果c是素数并且之前没有出现过,那么我们把它放入队列即可. int f[10001]; int v[10001]; void init()//素数筛 { memset(f,0,sizeof f); fo…
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /************************************************ Author :Running_Time Created Time :2015-8-2 15:46:57 File Name :POJ_3126.cpp ****************************…
这题原来以为是某种匹配问题,后来好像说是强连通的问题. 做法:建图,每个方老师和它想要的缘分之间连一条有向边,然后,在给出的初始匹配中反向建边,即如果第i个方老师现在找到的是缘分u,则建边u->i.这样求出所有的强连通分量,每个强连通分量中方老师和缘分的数目一定是相等的,所以每个方老师一定可以找到与他在同一个强连通分量里的缘分,因为强连通分量中每个点都是可达的,某个方老师找到了其强连通分量中的非原配点,则该原配缘分一定可以在强连通分量中找到"新欢".可以画个图看看. 由于要构造非…
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方老师重新开了一家农场,农场一共有N个节点和M条双向边,但是有一个很大的问题就是有比如一个农场A到达农场B只有一条路径,问至少添加多少条边使得任意两个农场之间的路径多于一条. Input 多组数据,EOF结束. 第1行:N和M. 第2到第M+1行:每一行2个数Ui和Vi,表示Ui到…
方老师的分身 II Time Limit: 10000/5000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方老师计算出了走路时间最长的那个分身所用的时间.于是有个特殊的分身(据说是方老师真身!)就不想如此古板的走最短路了!由于这个分身的特殊性,这个分身对于单向边可以当双向边走.但是这个特殊的分身想走最短路的同时,要求至少经过k条边. Input 有多组数据 第一行两个整数n,m(1≤n…
方老师分身 I Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方老师为了开更多讲座,于是他分身了!早上他都在某一个教室分身,然后各个分身分别赶去各个不同的n个教室(当然每个教室都要有且只有一个分身).晚上各个分身都赶回之前分身时的教室,合并成一个人(不需要同时回去).但是教室间的路十分崎岖,而且是单向的.当然即便只是方老师的分身,那也是相当厉害的,…
老师买表 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 由于方老师出色的专题讲座,他的名气迅速扩散到全国各地,并通过各地的讲座赚到了很多钱,鉴于现在盛行买表,于是方老师带上了一个H×W的盒子去买表,我们假设每一个表占1×2或者2×1的空间,问方老师有多少种放置表的方式,把这个盒子填满. Input 输入有多组数据 每组数据占一行,每一行有2个正整数…
题目链接:https://vjudge.net/problem/UESTC-900   方老师炸弹 Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Status 方老师准炸毁学校,学校可以被看做是一个图包含NN个顶点和MM条边(顶点从00开始标号),方老师发明了一个方老师炸弹. 这个炸弹可以炸毁某一个节点和与这个节点相连的所有边.但是方老师现在很彷徨,他想使得使…
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-digit room numbers on their offices.  — It is a matter of security to change such things every now…
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…
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-digit room numbers on their offices. — It is a matter of security to change such things every now…