Prime Path(POJ 3126 BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15325 | Accepted: 8634 |
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
单纯的将所有情况都搜一遍
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
bool prime[];
bool vis[];
int n[];
int a,b;
struct node
{
int num,t;
};
void init()
{
int i,j;
memset(prime,,sizeof(prime));
for(i=;i<;i++)
{
for(j=;j<i;j++)
{
if(i%j==)
break;
}
if(j==i)
{
prime[i]=; //1代表质数
//cout<<i<<endl;
}
}
for(i=;i<;i++)
n[i]=i;
n[]=,n[]=,n[]=,n[]=;
return;
}
int bfs()
{
int i,j;
queue<node> Q;
node tem,u;
int k;
tem.num=a,tem.t=;
memset(vis,,sizeof(vis));
Q.push(tem);
while(!Q.empty())
{
tem=Q.front();
Q.pop();
//cout<<tem.num<<endl;
/*if(tem.num==3733)
printf("adsfadsf\n");*/
if(tem.num==b)
return tem.t;
for(i=;i<=;i++)
{
if(tem.num%==n[i])
continue;
u.num=tem.num/;
u.num=u.num*+n[i];
//cout<<u.num<<" "<<prime[u.num]<<" "<<vis[u.num]<<endl;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=(tem.num/)%;
if(k==n[i])
continue;
k=tem.num%;
u.num=((tem.num/)*+n[i])*+k;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=(tem.num/)%;
if(k==n[i])
continue;
k=tem.num%;
u.num=((tem.num/)*+n[i])*+k;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
Q.push(u);
}
}
for(i=;i<=;i++)
{
k=tem.num/;
if(k==n[i])
continue;
//cout<<k<<" "<<n[i]<<endl;
k=tem.num%;
u.num=n[i]*+k;
// cout<<u.num<<endl;
if(vis[u.num]==&&prime[u.num])
{
u.t=tem.t+;
vis[u.num]=;
//cout<<u.num<<endl;
Q.push(u);
}
}
//break;
}
return -;
}
int main()
{
int T,ans;
init();
freopen("in.txt","r",stdin);
//freopen("ou.txt","w",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
ans=bfs();
if(ans==-) printf("Impossible\n");
else printf("%d\n",ans);
}
}
Prime Path(POJ 3126 BFS)的更多相关文章
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
- Prime Path (POJ - 3126 )(BFS)
转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82697622 作者:Mercury_Lc 题目链接 题意:就是给你一个n, ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3126 Prime Path(筛法,双向搜索)
题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)
传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)——引用
在Django视图函数中经常出现类似于'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)的错误. ...
- DJANTO之FORM
文档很仔细,但熟悉要慢慢来~~ from django.shortcuts import render from contact.forms import ContactForm from djang ...
- jquery-ui datepicker使用
这是一款老外设计的日期控件 很多显示方式都是国外的 需要自己调整一下 closeText: "Done", prevText: "上一月", nextText: ...
- 【PAT L2-001】最短路计数
给定一个无向带权网络,无负边,无重边和自环,每个顶点有一个正数权值.首先求特定原点s到终点d的最短路的个数:然后求所有最短路中顶点权值a[i]之和最大的那条,输出这条路径. 可用dijkstra算法求 ...
- HTTPS证书生成原理和部署细节
看看下面,部分电信用户访问京东首页的时候,会看到右下角有一个浮动广告: 小白用户以为是京东有意放置的,细心的用户会发现,这个 iframe 一层嵌一层的恶心广告很明显是电信/中间人通过 DNS 劫持注 ...
- SQL server 中 COUNT DISTINCT 函数
目的:统计去重后表中所有项总和. 直观想法: SELECT COUNT(DISTINCT *) FROM [tablename] 结果是:语法错误. 事实上,我们可以一同使用 DISTINCT 和 C ...
- SVN版本分支合并
SVN,开发中常用的工具,也没什么可说的.这里只是记录一下,以免太久不用了想用的时候又忘了. 首先已经有两个目录,一个是分支目录SVNChild,一个是主干目录SVNMain.SVNChild是从SV ...
- python学习之路-1 python简介及安装方法
python简介 一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. 目前最新版本为3.5.1,发布于2015年12月07日 ...
- 盒子模型&position定位
有时候深深的感觉语文这门课程其实很有用, 至少以前学的时候没有感觉到 直到现在阅读大量的别人的资料文章的时候或者是看一些题目....... 总之:认真阅读小心品味 当然,前面的孤言自语和本文无关,只是 ...
- Working with Numbers in PL/SQL(在PL/SQL中使用数字)
This article gives you all the information you need in order to begin working with numbers in your P ...