题目链接: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. mysql查询死锁,执行语句,服务器状态等语句集合

    [转]http://blog.csdn.net/enweitech/article/details/52447006

  2. 中国余数定理 2(codevs 3990)

    题目描述 Description Skytree神犇最近在研究中国博大精深的数学. 这时,Sci蒟蒻前来拜访,于是Skytree给Sci蒟蒻出了一道数学题: 给定n个质数,以及k模这些质数的余数.问: ...

  3. 星球大战 BZOJ 1015

    星球大战 [问题描述] 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  4. Mac 快速修改 hosts 文件

    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

  5. R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)

    Modified: 27-Oct-2013 Type: HOWTO In this Document Goal Solution References APPLIES TO: BI Publisher ...

  6. BroadcastReceiver详解(一)

    今天我们来讲一下Android中BroadcastReceiver的相关知识. BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在And ...

  7. ftp服务器调用出错

    因工作需要,需从FTP服务器上下载文件,本地测试OK,服务器上在FTP服务器连接之后卡住,没有异常和提示信息,在打印FTP命令之后,显示:150 Opening data channel for fi ...

  8. IE浏览器打不开解决的方法

    windows 7和windows 8上的IE浏览器打不开.非常可能是权限问题,解决的方法: 点击"開始"-"执行",输入"regedit" ...

  9. ZOJ ACM 1314(JAVA)

    昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...

  10. 【基础练习】【线性DP】codevs3027 线段覆盖2题解

    文章被盗还是非常严重,加版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看看 这道题目是线性动归 可是思想和背包有些类似 事实上线性动 ...