POJ 3216 Prime Path(打表+bfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27132 | Accepted: 14861 |
Description

— 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
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Source
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 10100
int prime[max_v];
int vis[max_v];
struct node
{
int x,step;
};
void init()//打N以内的素数表,prime[i]=0为素数。
{
memset(prime,,sizeof(prime));
prime[]=;
for(int i=; i<max_v; i++)
{
if(prime[i]==)
{
for(int j=; j*i<max_v; j++)
prime[j*i]=;
}
}
}
int bfs(int s,int e)
{
int num;
memset(vis,,sizeof(vis));//vis[N]用来标记是否查找过
queue<node> q; node p,next; p.x=s;
p.step=; vis[s]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==e)
{
return p.step;
}
int t[];
t[]=p.x/;//千
t[]=p.x/%;//百
t[]=p.x/%;//十
t[]=p.x%;//个 for(int i=; i<=; i++)
{
int temp=t[i];//这里特别注意,每次只能变换一位数,这里用来保存该变换位的数,变换下一位数时,该位数要恢复
for(int j=; j<; j++)
{
if(t[i]!=j)
{
t[i]=j;//换数
num=t[]*+t[]*+t[]*+t[];
}
if(num>=&&num<=&&vis[num]==&&prime[num]==)//满足要求,四位数,没有用过,是素数
{
next.x=num;
next.step=p.step+;
q.push(next);
vis[num]=;
}
}
t[i]=temp;//恢复
}
}
return -;
}
int main()
{
//题意:给你两个4位数,要求你每次只能变换一位数字,并且变换前后的数字都要满足是素数;求最少变换次数;
//首先打好素数表,再进行BFS
int n,a,b,ans;
init();
cin>>n;
while(n--)
{
cin>>a>>b;
ans=bfs(a,b);
if(ans==-)
{
cout<<"Impossible"<<endl;
}
else
{
cout<<ans<<endl;
}
}
return ;
}
POJ 3216 Prime Path(打表+bfs)的更多相关文章
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- POJ 3126 Prime Path (素数+BFS)
题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- [POJ]P3126 Prime Path[BFS]
[POJ]P3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35230 Accepted: ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- 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)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
随机推荐
- Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控
目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 ...
- MyBatis 学习(一)
一.MyBatis 1.MyBatis 介绍(百度) MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数 ...
- asp.net 日期转换为大写汉字
//年份转换为大写汉字 public static string numtoUpper(int num) { return "零壹贰叁肆伍陆柒捌玖"[num].ToString() ...
- sql中replace的用法
update 表名 set 字段名=REPLACE (字段名,'原来的值','要修改的值') 如:将tbl_user表的user_name字段中的大写的A替换成小写的a update tbl_stud ...
- [转载]hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- Raspberry 安装vstudio
Visual Studio Code微软公司推出的一款轻量级的Visual Studio风格的跨平台的IDE.当然,除了Windows,OSX,还能在树莓派上使用.目前树莓派上可用的IDE真不多,VS ...
- Fiddler给网站“优化”
最近访问某知名网站的速度非常慢,有时候需要2分钟还没完全打开,页面展示了一半就卡住,然后等半天才继续显示下面部分.这种情况已经有几个月了,不知道是他们服务器原因还是我所在网络的问题,但是基本上在其他网 ...
- 沙盒 sandbox 简记随笔
沙盒又称沙箱(sandbox),是一种按照 安全策略 限制 程序行为 的 执行环境. “沙盒”技术的实践运用流程是: 1. 让疑似病毒文件的可疑行为在虚拟的“沙盒”里充分运行,“沙盒”会记下 ...
- hook的函数传入类
简单记录 比如要hook一个app包中一个类的private void c(dmp dmp1),其中dmp是个类,这种的处理的方式如下: 用cydiasubstrate hook框架 1.先通过hoo ...
- QT开发(二) windows下简单部署
如果使用vs编译器 需要c runtime 例如(msvc110 )这种 还需要若干qt的dll 基本在qt的bin目录 如果使用了QWindow这种对象还需要引用qt目录 plugins下 的内容 ...