Prime Path
 

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 题意 将a转换到b,每次换一位,每次交换后的数都是素数,算最短换了多少次
我的思路
找到所有的四位数的素数,打表
用bfs 有四十个方向,找到答案
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int prime[],n,m,tail,head;
struct que
{
int x,time;
}que[];
int row(int x)
{
int i,s=;
for(i=;i<=x;i++)
s*=;
return s;
}
int bfs()
{
int i,j,x;
for(i=;i<=;i++)
{
for(j=;j<=i/;j++)
if(i%j==)break;
if(j==i/+)prime[i]=;
else prime[i]=;
}
prime[n]=;
tail=head=;
que[].x=n,que[].time=;
while(head<=tail)
{
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
x=que[head].x-(que[head].x/row(i-)%)*row(i-)+j*row(i-);
if(x>=&&x<=&&prime[x])
{
que[++tail].x=x;
que[tail].time=que[head].time+;
if(x==m)return que[tail].time;
else prime[x]=;
}
}
}
head++;
}
return ;
}
int main()
{
int i,j,key,g;
while(scanf("%d",&g)==)
{
for(i=;i<g;i++)
{
scanf("%d %d",&n,&m);
if(n==m)
{
printf("0\n");
continue;
}
key=bfs();
if(key)
printf("%d\n",key);
else
printf("Impossible\n");
}
}
return ;
}
												

poj3126 Prime Path(c语言)的更多相关文章

  1. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  2. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  3. POJ3126 Prime Path —— BFS + 素数表

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

  4. POJ3126 Prime Path

    http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...

  5. POJ3126 Prime Path(BFS)

    题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  6. POJ3126——Prime Path

    非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...

  7. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  8. Prime Path POJ-3126

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

  9. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

随机推荐

  1. Python实例---模拟微信网页登录(day5)

    第六步: 实现发送/接受消息---day5代码 settings.py """ Django settings for weixin project. Generated ...

  2. 【软件工程1916|W(福州大学)_助教博客】团队第四次作业(第7次)成绩公示

    1. 作业链接: https://edu.cnblogs.com/campus/fzu/SoftwareEngineering1916W/homework/2874 2. 评分准则: 本次作业评分分为 ...

  3. 报数的golang实现

    报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: . . . . . 1 被读作 "one 1" ("一个一") , 即 11 ...

  4. 【数据库】事务,ACID,CAP和一致性

    什么是事务 事务是指由一系列数据库操作组成的一个完整的逻辑过程,这个过程中的所有操作要么都成功,要么都不成功.比如:常见的例子就是银行转账的例子,一次转账操作会包含多个数据库操作,而这些数据库操作需要 ...

  5. redis 基本命令

    set times 2017.12.12  设置键名键值 get times  获取键名 exists times 检测键名是否存在 type times 键名的类型 expire times 6   ...

  6. (一 ) 天猫精灵接入Home Assistant-服务器搭建

    ()1登陆阿里云 https://ecs.console.aliyun.com/?spm=5176.2020520001.0.0.6B1Uov#/home 账号dongdongmqtt 密码***** ...

  7. go标准库的学习-text/template

    参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...

  8. go标准库的学习-net

    参考:https://studygolang.com/pkgdoc 导入方式: import "net" net包提供了可移植的网络I/O接口,包括TCP/IP.UDP.域名解析和 ...

  9. Spring定时任务使用和如何使用邮件监控服务器

    Spring相关的依赖导入进去,即可使用spring的定时任务! <!-- spring核心包 --> <dependency> <groupId>org.spri ...

  10. LINUX 手动建立SWAP文件及删除

    原文:http://blog.itpub.net/7728585/viewspace-670175/ 1. 确定swap文件的大小,单位为M.将该值乘以1024得到块大小.例如,64MB的swap文件 ...