POJ 3126:Prime Path(素数+BFS)
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 103310331033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 817981798179 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 888, 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 103310331033 to 817981798179 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 666 pounds. Note that the digit 111 which got pasted over in step 222 can not be reused in the last step – a new 111 must be purchased.
Input
One line with a positive number: the number of test cases (at most 100100100). 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.
Examples
Input
3
1033 8179
1373 8017
1033 1033
Output
6
7
0
题意
给出两个四位的素数n,mn,mn,m,要求nnn每次只能变换一位,并且变换后的数字依旧是素数。
求nnn经过多少步变换能够变成mmm;如果nnn无法变成mmm,输出Impossible
思路
将nnn的四位数字拆分了,每次变换一位,来判断是否符合条件,如果符合条件,将新数字加入队列,至到数字和mmm相等,或队列为空
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const int maxn=1e4+10;
const int mod=1e9+7;
using namespace std;
int vis[maxn];
int _vis[maxn];
int cnt;
struct node
{
int num;
int step;
};
int bfs(int n,int m)
{
ms(_vis,0);
int newnum;
node p,q;
queue<node>que;
p.num=n;
p.step=0;
que.push(p);
_vis[n]=1;
while(!que.empty())
{
q=que.front();
que.pop();
if(q.num==m)
return q.step;
int get[4];
int N=q.num;
int _=0;
while(N)
{
get[_++]=N%10;
N/=10;
}
for(int i=0;i<4;i++)
{
int __=get[i];
for(int j=0;j<=9;j++)
{
if(get[i]!=j)
{
get[i]=j;
newnum=get[0]+get[1]*10+get[2]*100+get[3]*1000;
}
if(!vis[newnum]&&newnum>=1000&&newnum<10000&&!_vis[newnum])
{
p.num=newnum;
p.step=q.step+1;
_vis[newnum]=1;
que.push(p);
}
}
get[i]=__;
}
}
return -1;
}
void init()
{
vis[0]=vis[1]=1;
for(int i=2;i<maxn;i++)
{
if(!vis[i])
{
for(int j=2;j*i<maxn;j++)
vis[i*j]=1;
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
double _begin_time = clock();
#endif
init();
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int ans=bfs(n,m);
if(ans==-1)
cout<<"Impossible"<<endl;
else
cout<<ans<<endl;
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %lf ms.", _end_time - _begin_time);
#endif
return 0;
}
POJ 3126:Prime Path(素数+BFS)的更多相关文章
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- 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)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- poj 3126 Prime Path 【bfs】
题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path【BFS】
<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四 ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- 用原生CSS编写-怦怦跳的心
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Yarn的Tool接口案例
目录 Yarn的Tool接口案例 Tool接口环境准备 1 新建Maven项目YarnDemo 编写代码 打包jar上传到集群 Yarn的Tool接口案例 Tool接口环境准备 之前写wordcoun ...
- 运算符重载+日期类Date
Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...
- 日常Java 2021/10/28
Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...
- A Child's History of England.29
You have not forgotten the New Forest which the Conqueror made, and which the miserable people whose ...
- day20 系统优化
day20 系统优化 yum源的优化 yum源的优化: 自建yum仓库 使用一个较为稳定的仓库 # 安装华为的Base源 或者使用清华的源也可以 wget -O /etc/yum.repos.d/Ce ...
- 零基础学习java------day27-28---------电影评分数据案例,. RPC案例
一. 电影评分数据案例 movie:电影id rate:用户评分 timeStamp:评分时间 uid:用户id 简化数据: 需求: (1)每个用户评分最高的3部电影 (2)每个用户评分的平均值 ( ...
- 对于Linq关键字和await,async异步关键字的扩展使用
最近在看neuecc大佬写的一些库:https://neuecc.medium.com/,其中对await,async以及linq一些关键字实现了自定义化使用, 使其不需要引用对应命名空间,不需要多线 ...
- 【编程思想】【设计模式】【行为模式Behavioral】状态模式State
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/state.py #!/usr/bin/env pytho ...
- shiro免认证的路径配置
– ?:匹配一个字符,如/admin? 将匹配/admin1,但不匹配/admin 或/admin/:– *:匹配零个或多个字符串,如/admin 将匹配/admin./admin123,但不匹配/a ...