poj3216 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 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
3
1033 8179
1373 8017
1033 1033 Sample Output
6
7
0
题意:给你四位数字a,b;每一不只能改变一位数字,且新的数字只能是素数,
要你输出最小步数 题解:bfs,每次向下遍历40个方向 代码:
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
struct niu
{
int prime,step;
niu(){}
niu(int pr,int st)
{
prime=pr,step=st;
}
};
int a,b;
int ans=INF;
bool check(int a)
{
for(int i=;i*i<=a;i++)
if(a%i==)return false;
return a!=;
}
queue<niu>q;
bool vis[];
int bfs()
{
memset(vis,false,sizeof(vis));
while(q.size())q.pop();
q.push(niu(a,));
vis[a]=true;
while(q.size())
{
niu tmp=q.front();q.pop();//cout<<tmp.prime<<tmp.step<<endl;
if(tmp.prime==b)
{
ans=min(ans,tmp.step);
}
int cnt=tmp.prime%;
int cur=(tmp.prime/)%;
for(int i=;i<=;i++)
{
int nx=(tmp.prime/)*+i;
if(check(nx)&&!vis[nx])
{
vis[nx]=true;
q.push(niu(nx,tmp.step+));
}
int ny=(tmp.prime/)*+i*+cnt;
if(check(ny)&&!vis[ny])
{
vis[ny]=true;
q.push(niu(ny,tmp.step+));
}
int nz=(tmp.prime/)*+i*+cur*+cnt;
if(check(nz)&&!vis[nz])
{
vis[nz]=true;
q.push(niu(nz,tmp.step+));
}
if(i==)continue;
int nn=(tmp.prime%)+i*;//cout<<nn<<endl;
if(check(nn)&&!vis[nn])
{
vis[nn]=true;
q.push(niu(nn,tmp.step+));
}
}
}
return ans==INF?-:ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ans=INF;//注意这里的ans要初始化
cin>>a>>b;
if(bfs()==-)
cout<<"Impossible"<<endl;
else cout<<bfs()<<endl;
}
return ;
poj3216 Prime Path(BFS)的更多相关文章
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- 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 POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ-3126-Prime Path(BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27852 Accepted: 15204 Desc ...
随机推荐
- Django学习——开发你的第一个Django应用1
突然对Django热情似火,所以就开学习了,我是根据官方文档学习的,所以我打算把官方文档翻译一遍,全当学习,首先贴官方文档的地址:https://docs.djangoproject.com/en/1 ...
- 2018-2-13-win10-uwp-BadgeLogo-颜色
title author date CreateTime categories win10 uwp BadgeLogo 颜色 lindexi 2018-2-13 17:23:3 +0800 2018- ...
- quota - 显示磁盘的使用和限额
总览 (SYNOPSIS) quota [ -guv | q ] quota [ -uv | q ] user quota [ -gv | q ] group 描述 (DESCRIPTION) Quo ...
- css day1
基础知识 css:层叠样式表 以html为基础,提供丰富的功能,如字体.颜色.背景的控制及整体排版 css中只有(冒号): 没有(等于号)= css样式规则 1.选择器用于指定css样式作用的htm ...
- Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...(转)
对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...
- 【串线篇】spring boot配置文件大全【下】
一.配置文件占位符 1.1.随机数 ${random.value}.${random.int}.${random.long} ${random.int(10)}.${random.int[1024,6 ...
- 存储过程如何传变量到like下
存储过程中执行如下DDL语句create or replace procedure etl_test(v_com varchar2) is v_spname varchar2(40); com var ...
- max_length 属性
错误:漏掉了 max_length 属性 ERRORS:users.UserProfile.image: (fields.E210) Cannot use ImageField because Pi ...
- leetcode-167周赛-1290-二进制链表转整数
题目描述: 提交: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val ...
- Spring Security 安全认证
Spring Boot 使用 Mybatis 依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> ...