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 ...
随机推荐
- Android-1
@String 支持多语言 layout中的text文本中String都尽量定义在String.xml中,便于多语言管理. <resources> <string name=&quo ...
- 原 iOS面试题收集
原 iOS面试题收集 发表于2年前(2013-07-22 13:47) 阅读(369) | 评论(0) 4人收藏此文章, 我要收藏 赞0 听云性能监测产品App.Server.CDN免费试用,绑定 ...
- [POJ] 1064 Cable master (二分查找)
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...
- 在C51中如何实现软复位?
可以定义一个指向复位向量(0x0000)的函数指针,然后在C程序中需要软复位的地方调用该函数: ((void (code *) (void)) 0x0000) (); 例如,以下程序不断地复位: vo ...
- centos 图形界面和命令行界面切换
如果在图形界面下,按:Ctrl+Alt+F2进入命令行登录界面 切到root用户下, su root password 1, 关闭图形界面: init 3 关闭图形界面(XServer服务也会关闭) ...
- mysql命令行的基本用法
基础介绍:1.在linux下使用下列命令,请确认mysql的bin目录是否已经加入到PATH路径中,或者是已经进入到mysql安装路径下的bin目录查看PATHshell> echo $PATH ...
- CentOS下安装无线网卡驱动 (转)
1. 确定自己的网卡和内核版本:lspci | grep Network #根据输出的信息确定网卡的型号.uname -a #确定内核版本 2. 配置yum使用RPMForg ...
- Unity 异步加载场景
效果图如下: 今天一直在纠结如何加载场景,中间有加载画面和加载完毕的效果动画! A 场景到 B , 看见网上的做法都是 A –> C –> B. C场景主要用于异步加载B 和 播放一些 ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- How to uninstall (remove) JAVA from OS X Lion
Open terminal (Applications -> Utilities -> Terminal) To remove JVM enter folowing: sudo rm -r ...