CD0J/POJ 851/3126 方老师与素数/Prime Path BFS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9982 | Accepted: 5724 |
Description
![](http://poj.org/images/3126_1.jpg)
— 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
Then for each test case, one line with two numbers separated by a
blank. Both numbers are four-digit primes (without leading zeros).
Output
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的更多相关文章
- cdoj 851 方老师与素数 bfs
方老师与素数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- 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(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- [POJ]P3126 Prime Path[BFS]
[POJ]P3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35230 Accepted: ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
随机推荐
- 如何用Percona XtraBackup进行MySQL从库的单表备份和恢复【转】
前提 应该确定采用的是单表一个表空间,否则不支持单表的备份与恢复. 在配置文件里边的mysqld段加上 innodb_file_per_table = 1 环境说明: 主库:192.168.0.1 从 ...
- ASP.NET MVC 防止跨站请求伪造(CSRF)攻击的方法
在HTTP POST请求中,我们多次在View和Controller中看下如下代码: View中调用了Html.AntiForgeryToken(). Controller中的方法添加了[Valida ...
- C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
运行时类型识别(RTTI)的引入有三个作用: 配合typeid操作符的实现: 实现异常处理中catch的匹配过程: 实现动态类型转换dynamic_cast. 1. typeid操作符的实现 1.1. ...
- [转载]如何在C++03中模拟C++11的右值引用std::move特性
本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...
- mysql -> 简介&体系结构_01
数据库简介 数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据运行新增.截取.更新.删除等操作. 所谓“数据库”系以一定方式储存在一起.能予多个用户共享.具有尽可能小的 ...
- python 写入execl记录
记录代码中关于写execl的操作 # 创建execl workbook = xlwt.Workbook(encoding='utf8') # 创建样式实例 style = xlwt.XFStyle() ...
- Owin WebApi版本控制
public class WebApiControllerSelector : IHttpControllerSelector { private const string NamespaceKey ...
- css-概述和选择器
一:CSS 概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与 ...
- python网络编程-socket样例
socket样例 一:只能一个客户端发送一个信息. 客户端 # -*- coding:utf-8 -*- __author__ = 'shisanjun' import socket sock=soc ...
- table中的td等长(不随内容大小变化)
使用的table时候发现td的长度是随着内容的大小而变化的,但是有的时候我们不希望这样.想要td等长可以在 tbale中加上 style=“table-layout:fixed” ...