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-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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff; //无限大
const int MAXN = ;
bool flag[MAXN];
int primes[MAXN], pi;
struct point
{
int x;
int y;
};
void GetPrime_1()
{
int i, j;
pi = ;
memset(flag, false, sizeof(flag));
for (i = ; i < MAXN; i++)
if (!flag[i])
{
primes[i] = ;//素数标识为1
for (j = i; j < MAXN; j += i)
flag[j] = true;
}
}
int vis[maxn];
int main()
{
GetPrime_1();
int t;
cin>>t;
while(t--)
{
memset(vis,,sizeof(vis));
int n,m;
cin>>n>>m;
vis[n]=;
queue<point> q;
q.push((point){n,});
int flag1=;
while(!q.empty())
{
point now=q.front();
if(now.x==m)
{
flag1=now.y;
break;
}
point next;
for(int i=;i<=;i++)
{
next.x=now.x/;
next.x*=;
next.x+=i;
next.y=now.y+;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push(next);
} }
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
//cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
for(int i=;i<=;i++)
{
int temp=now.x%;
next.x=now.x/;
next.x*=;
next.x+=i*;
next.x+=temp;
if(next.x<||next.x>=)
continue;
if(vis[next.x]==)
continue;
if(next.x==m)
{
flag1=next.y;
break;
}
if(primes[next.x]==)
{
// cout<<next.x<<endl;
vis[next.x]=;
q.push((point){next.x,now.y+});
}
}
if(flag1>)
break;
q.pop();
}
printf("%d\n",flag1);
}
return ;
}

CD0J/POJ 851/3126 方老师与素数/Prime Path BFS的更多相关文章

  1. cdoj 851 方老师与素数 bfs

    方老师与素数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  2. poj 3126 Prime Path bfs

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

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

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

  4. [POJ]P3126 Prime Path[BFS]

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

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

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

  6. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  7. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  8. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  9. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

随机推荐

  1. SELinux 入门【转】

    一.前言 安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统.SELinux 主要由美国国 ...

  2. python 中的__del__

    # -*- coding: utf-8 -*- # @Time : 2018/9/19 20:21 # @Author : cxa # @File : delDemo.py # @Software: ...

  3. avalonJS-源码阅读(一)

    写angularJS源码阅读系列的时候,写的太垃圾了.一个月后看,真心不忍直视,以后有机会的话得重写.这次写avalonJS,希望能在代码架构层面多些一点,少上源码.多写思路. avalon暴露句柄方 ...

  4. linux limits研究

    ---------------------------------------------------------------------------------------------------- ...

  5. 排序与相关性(Sorting and Relevance)

    本文翻译自Elasticsearch官方指南的Sorting and Relevance一章的第一节. 原文地址:http://www.elastic.co/guide/en/elasticsearc ...

  6. lucene-利用内存中索引和多线程提高索引效率

    转载地址: http://hi.baidu.com/idoneing/item/bc1cb914521c40603e87ce4d 1.RAMDirectory和FSDirectory对比 RAMDir ...

  7. JAVA随笔(三)

    私有是针对类的,而不是对象. static 函数,其实是类函数.之前一直不太理解每个类中的static main是什么意思,为什么main中不能直接调用非静态的变量:因为main是 类函数,不是属于某 ...

  8. thinkphp模型创建

  9. C++ 实现memcpy和strcpy

    /** * @Method: Memcpy * @Access: public * @Return: void * * @Param : dst - 目的起始地址 * @Param : src - 源 ...

  10. grail开发环境的搭建

    本文参考:Grails入门指南(第二版) 1. 下载jdk和Grail http://www.oracle.com/technetwork/java/javase/downloads/ http:// ...