题目链接:http://poj.org/problem?id=3126

Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22936   Accepted: 12706

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

Source

题解:

1.打印素数表。

2.由于只有四位数,直接枚举不会超时。由于要求的是“最少步数”,所以用BFS进行搜索。

写法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node //num为素数,dig[]为这个素数的每个位上的数,便于操作。
{
int num, dig[], step;
}; int vis[], pri[]; queue<node>que;
int bfs(node s, node e)
{
ms(vis,);
while(!que.empty()) que.pop();
s.step = ;
vis[s.num] = ;
que.push(s); node now, tmp;
while(!que.empty())
{
now = que.front();
que.pop(); if(now.num==e.num)
return now.step; for(int i = ; i<; i++) //枚举位数
for(int j = ; j<; j++) //枚举数字
{
if(i== && j==) continue; //首位不能为0
tmp = now;
tmp.dig[i] = j; //第i为变为j
tmp.num = tmp.dig[] + tmp.dig[]*+tmp.dig[]*+tmp.dig[]*;
if(!pri[tmp.num] && !vis[tmp.num]) //num为素数并且没有被访问
{
vis[tmp.num] = ;
tmp.step++;
que.push(tmp);
}
}
}
return -;
} void init() //素数表,pri[]==0的为素数
{
int m = sqrt(+0.5);
ms(pri,);
pri[] = ;
for(int i = ; i<=m; i++) if(!pri[i])
for(int j = i*i; j<=; j += i)
pri[j] = ;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n, m;
node s, e;
scanf("%d%d",&n,&m);
s.num = n; e.num = m;
for(int i = ; i<; i++, n /= ) s.dig[i] = n%;
for(int i = ; i<; i++, m /= ) e.dig[i] = m%; int ans = bfs(s,e);
if(ans==-)
puts("Impossible");
else
printf("%d\n",ans);
}
}

写法二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int num, step;
}; int vis[], dig[];
int pri[]; queue<node>que;
int bfs(int s, int e)
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.num = s;
now.step = ;
vis[now.num] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.num==e)
return now.step; dig[] = now.num%;
dig[] = (now.num/)%;
dig[] = (now.num/)%;
dig[] = (now.num/)%;
for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
if(i== && j==) continue;
tmp.num = now.num + (j- dig[i])*pow(,i); //pow前面不要加上强制类型(int)
// tmp.num = now.num + (j- dig[i])* (int)pow(10,i);
// (int)pow(10,2) 居然等于99, 看来还是不要依赖这些函数
if(!pri[tmp.num] && !vis[tmp.num])
{
vis[tmp.num] = ;
tmp.step = now.step + ;
que.push(tmp);
}
}
}
return -;
} void init()
{
int m = sqrt(+0.5);
ms(pri,);
pri[] = ;
for(int i = ; i<=m; i++) if(!pri[i])
for(int j = i*i; j<=; j += i)
pri[j] = ;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n, m;
scanf("%d%d",&n,&m);
int ans = bfs(n,m);
if(ans==-)
puts("Impossible");
else
printf("%d\n",ans);
}
}

POJ3126 Prime Path —— BFS + 素数表的更多相关文章

  1. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

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

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

  3. POJ3126 Prime Path(BFS)

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

  4. poj3126 Prime Path 广搜bfs

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

  5. POJ2126——Prime Path(BFS)

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

  6. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  7. poj 3126 Prime Path bfs

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

  8. poj3126 Prime Path(c语言)

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

  9. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

随机推荐

  1. 前端ui框架---ant 蚂蚁金服开源

    蚂蚁金服和饿了么好像不错 饿了么官网:http://element.eleme.io/#/zh-CN饿了么github:http://github.com/elemefe 蚂蚁金服  https:// ...

  2. Wiley出版 SQL Server 2005宝典

    原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] Wiley出版 SQL Server 2005宝典 迅雷专用高速下载    thunder://QUFmdHA6L ...

  3. msp430项目编程46

    msp430综合项目---监控系统46 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  4. HBase总结

    1.HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google ...

  5. Spring在Bean中注入集合

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-collection.html: 如果你想传递多个值,如Java Collect ...

  6. 你还在为移动端选择器picker插件而捉急吗?

    http://www.cnblogs.com/jingh/p/6381079.html 开题:得益于项目的上线,现在终于有时间来写一点点的东西,虽然很浅显,但是我感觉每经历一次项目,我就学到了很多的东 ...

  7. grafana结合influxdb、open-falcon出图配置

    1.https://www.jianshu.com/p/fadcf4d92b0e 2.https://www.jianshu.com/p/21ce6ee143f3 3.http://www.super ...

  8. Qt5官方demo解析集30——Extending QML - Binding Example

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...

  9. cms完整视频教程+源码 孔浩老师 全131讲

    话不多说,请看如下链接,  项目在此文件夹目录下:  JAVA专区>3.深入Java Web>3.1.cms项目 很多反馈说无效 本次 2016.09.12 发布最新链接 链接:https ...

  10. CentOS下常用的 19 条命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...