POJ:3126-Prime Path
题目链接:http://poj.org/problem?id=3126
Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25215 Accepted: 13889
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 and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
解题心得:
- 题意就是一个长官的房间号是n(四位数),你每次可以改变n中的一个数,要你改变次数最少将n变成m,并且在改变工程中所有的数字都必须是一个素数,并且都是没有前导零的四位数,如果不能通过这样的改变达到m则输出-1;
- 就是一个搜索加素数判断,直接bfs,只不过在入队的时候判断一下是否是一个素数就行了。
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1e4+100;
bool prim[maxn];
int n,m;
struct NODE {
int va,step;
NODE() {
va = 0,step = 0;
}
}now2,Next2;
void get_prim() {
prim[0] = prim[1] = true;
for(int i=2;i<maxn;i++) {
if(prim[i])
continue;
for(int j=i*2;j<maxn;j+=i) {
prim[j] = true;
}
}
}
int bfs() {
bool vis[maxn];
int now[6],Next[6];
memset(vis,0, sizeof(vis));
vis[n] = true;
queue <NODE> qu;
now2.va = n,now2.step = 0;
qu.push(now2);
while(!qu.empty()) {
int temp = qu.front().va;
int step = qu.front().step;
qu.pop();
if(temp == m)
return step;
int k = 0;
while(k < 4) {
now[k] = temp%10;
temp /= 10;
k++;
}
for(int i=0;i<4;i++) {
for(int j=1;j<=9;j++) {
for(int k=0;k<4;k++)
Next[k] = now[k];
Next[i] = (now[i] + j) % 10;
int num = 0;
for(int k=3;k>=0;k--) {
num = num*10 + Next[k];
}
if(!prim[num] && !vis[num] && num >= 1000) {
Next2.step = step+1;
Next2.va = num;
qu.push(Next2);
vis[num] = true;
}
}
}
}
return -1;
}
int main() {
int t;
scanf("%d",&t);
get_prim();
while(t--) {
scanf("%d%d",&n,&m);
int ans = bfs();
if(ans != -1)
printf("%d\n",ans);
else
printf("Impossible\n");
}
return 0;
}
POJ:3126-Prime Path的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
随机推荐
- 从零开始的全栈工程师——js篇(js的异步)
js中的异步 Javascript语言的执行环境是"单线程"(single thread,就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任 ...
- JSON FILE NOT FOUND?
(WIN7+IIS7) 搞一个小测试,一个小程序,用一个JSON文件来显示数据, 用HTML 访问是完全没有问题的,可是一放到IIS下,就出现无法访问, CONSOLE:提示 NOT FOUND 原 ...
- SharePoint 计时器作业
本文将介绍 SharePoint 2010 的默认计时器作业,即我们通常说的Timer服务.计时器作业在 SharePoint Server 的特定 Windows 服务中运行.计时器作业还是执行定时 ...
- python标准库 - socket
地址簇(address family) socket.AF_UNIX # (UNIX Domain Sockets) socket.AF_INET # ipv4 socket.AF_INET6 # i ...
- 数据结构(C#):图的最短路径问题、(Dijkstra算法)
今天曾洋老师教了有关于图的最短路径问题,现在对例子进行一个自己的理解和整理: 题目: 要求:变成计算出给出结点V1到结点V8的最短路径 答: 首先呢,我会先通过图先把从V1到V8的各种路径全部计算下来 ...
- 双击易语言没有反应,按住shift再双击可解决
参考资料:http://tieba.baidu.com/p/2987732743 的7楼.
- CSS:响应式下的折叠菜单(条纹式)
原文:CSS: Responsive Navigation Menu 译文:CSS:响应式导航菜单 译者:dwqs 写在之前,关于如何制作响应式的下拉菜单:响应式下的下拉菜单 之前,我写了一篇关于怎么 ...
- ERROR 3077 (HY000): To have multiple channels, repository cannot be of type FILE; Please check the repository configuration and convert them to TABLE.
在5.7.16搭建多源复制时,出现如下错误: mysql> change master to master_host='192.168.56.156',master_user='repl', ...
- 20145238-荆玉茗 《Java程序设计》第8周学习总结
20145238 <Java程序设计>第8周学习总结 教材学习内容总结 第15章 通用API 15.1.1 ·java.util.logging包提供了日志功能相关类与接口,使用日志的起点 ...
- js控制select选中显示不同表单内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...