POJ 3126 math(BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21581 | Accepted: 11986 |
Descripti
— 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
Source
#include "cstdio"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "stdlib.h"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e4+;
const int mod=1e9+;
const ll inf=1e16+;
bool pri[N];
bool u[N],v[N];
int c[N];//统计步数
void init(){
int i,j;
for(i=;i<=N;i++){
for(j=;j<i;j++)
if(i%j==){
pri[i]=;
break;
}
if(j==i) pri[i]=;
}
}
int bfs(int s,int e){
queue<int >q;
memset(v,, sizeof(v));
memset(c,, sizeof(c));
int tmp,a[],ans=s;
q.push(s);
v[s]=;
while(q.size()){
int k=q.front();
q.pop();
a[]=k/,a[]=k/%,a[]=k/%,a[]=k%;
for(int i=;i<;i++){
tmp=a[i];
for(int j=;j<;j++){
if(tmp!=j){
a[i]=j;
ans=a[]*+a[]*+a[]*+a[];
if(pri[ans]&&!v[ans]){//为素数且未使用过
c[ans]=c[k]+;
v[ans]=;
q.push(ans);
}
if(ans==e) {
printf("%d\n",c[ans]);
return ;
}
}
a[i]=tmp;
}
}
if(k==e) {
printf("%d\n",c[k]);
return ;
}
}
printf("Impossible\n");
return ;
}
int main()
{
int n;
int x,y;
scanf("%d",&n);
init();
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
bfs(x,y);
}
return ;
}
POJ 3126 math(BFS)的更多相关文章
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- Z1. 广度优先搜索(BFS)解题思路
/** BFS 解题思路 特点:从某些特定的节点开始,感染相邻的节点; 被感染的节点,再感染其相邻的节点,以此类推. 题目常见于数据结构包括 二维数组.树.图 **/ /** 1). 二维数组特定节点 ...
- Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)
Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. ...
- Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)
Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- SDUT-2139_从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在古老的魔兽 ...
- 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)
参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...
随机推荐
- python_day5--->递归函数,二分法查找
li = [1, 5, 6, 7, 12, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333]def er(num,li): if len(li) ==0: ...
- Java IO流之对象流
对象流 1.1对象流简介 1.2对象流分类 输入流字节流处理流:ObjectInputStream,将序列化以后的字节存储到本地文件 输出流字节流处理流:ObjectOutputStream 1.3序 ...
- Python内置函数进制转换的用法
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- STL语法——映射:map 反片语(Ananagrams,UVa 156)
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- SpringMVC 整合Jackson报错
最近用spring4.x整合Jackson,结果莫名其妙的一直报错,网上收索的结果都是在maven或者gradle的环境下配置依赖条件解决的:但是eclipseIDE环境下的jar包应该是会自动依赖影 ...
- [Lucene]-Lucene基本概述以及简单实例
一.Lucene基本介绍: 基本信息:Lucene 是 Apache 软件基金会的一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎.Luc ...
- loadrunner学习理论之一
1.负载测试.压力测试的区别? 答:负载测试是在被测系统所承受的正常范围内进行的 压力测试可以在极端的条件下进行 2.loadrunner的三大组件是什么,有什么作用? 答:虚拟用户生成器(virtu ...
- JS 点击复制按钮 将文字复制到手机剪贴板
我们在制作移动端网页的时候,经常会遇到这样一个问题,如何点击一个"复制"按钮,把一串文字复制到手机剪贴板,如上图所示. 看了网上的一些方法后,感觉那些方法都太复杂,有点要用插件,有 ...
- [1] Spring.Net
开发框架之Spring.Net
- ffmpeg最全的命令参数
Hyper fast Audio and Video encoderusage: ffmpeg [options] [[infile options] -i infile]... {[outfile ...