Prime Path--POJ3126(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
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0 题意:给你两个四位数m和n,求m变到n至少需要几步;每次只能从个十百千上改变一位数字,并且改变后的数要是素数;
我们可以用优先对列做;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#define N 11000
using namespace std;
int prime(int n)
{
int i,k;
k=(int)sqrt(n);
for(i=;i<=k;i++)
if(n%i==)
return ;
return ;
}
struct node
{
int x,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
int bfs(int m,int n)
{
int a[]={,,,};
int vis[N];
priority_queue<node>Q;
node q,p;
memset(vis,,sizeof(vis));
vis[m]=;
q.x=m;
q.step=;
Q.push(q);
while(!Q.empty())
{
q=Q.top();
Q.pop();
if(q.x==n)
return q.step;
for(int i=;i<;i++)//控制改变的哪一位
{
for(int j=;j<;j++)//把相对应的那一位变成j;
{
int L=q.x/(a[i]*);
int R=q.x%(a[i]);
p.x=L*(a[i]*)+j*a[i]+R;//重新组成的数;
if(p.x>=&&vis[p.x]==&&prime(p.x)==)
{
vis[p.x]=;
p.step=q.step+;
Q.push(p);
}
}
}
}
return -;
} int main()
{
int T,m,n,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
ans=bfs(m,n);
if(ans==-)
printf("Impossible\n");
else
printf("%d\n",ans);
}
return ;
}
Prime Path--POJ3126(bfs)的更多相关文章
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- (简单) POJ 3126 Prime Path,BFS。
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path[POJ3126] [SPFA/BFS]
描述 孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了.zydsg要做些改变,他想去和素数小姐姐约会. 所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样 ...
随机推荐
- 给sharepoint某列表项单独赋予权限
/// <summary> /// 列表项事件 /// </summary> public class EventReceiver2 : SPItemEventReceiver ...
- error: pathspec 'master' did not match any file(s) known to git.
问题描述: 在远程服务器上新建裸仓库git --bare init : git clone裸仓库到本地: 本地新建并切换分支xccdev,git checkout -b xccdev: 从xccde ...
- (iOS)判断GPS坐标是否在中国
博文转载至 http://blog.csdn.net/cuibo1123/article/details/45691631 火星坐标经纬度范围 由于火星坐标问题,所以需要判断一下经纬度是否在中国. 基 ...
- 使用dom4j解析xml为json对象
import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j. ...
- iOS - 白名单应用间相互跳转
1. 应用间相互跳转简介 在iOS开发的过程中,我们经常会遇到需要从一个应用程序A跳转到另一个应用程序B的场景.这就需要我们掌握iOS应用程序之间的相互跳转知识. 下面来看看我们在开发过程中遇到的应用 ...
- C++中class与struct的区别(struct的类型名同时可以作为变量名)
通常我们知道的区别: (一)默认继承权限.如果不明确指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理: (二)成员的默认访问权限.class的成员默 ...
- mouseleave,mouseout 和mouseover ,mouseenter区别
鼠标离开事件: mouseleave:只有鼠标离开指定元素时才会触发; mouseout 鼠标离开指定元素或内部子元素都会触发; 鼠标在上事件: mouseover:只有鼠标进入指定元素时才会触发; ...
- Jenkins-Build Monitor View
现在上了jenkins的任务越来越多,查看起来很不方便,想搞个大视图,刚好jenkins本身支持这个功能. 功能: 一个独特的View, 可以将指定的Job,显示出来,当Job很多时,效果很好看 下载 ...
- Docker关联使用的一些工具:Clip名字服务(转载)
Clip名字服务 Clip(http://blog.puppeter.com/read.php?7)是一个名字服务C/S架构,它将传统的IP管理维度替换为名字服务即有意义可记忆的String.Clip ...
- 【CF662A】Gambling Nim 线性基
[CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...