poj3126--Prime Path(广搜)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11751 | Accepted: 6673 |
Description

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
给出两个数s,e,都是素数。经过转化,将s转化为e的最小步数
规则,每一次仅仅能改动一位,每次得到的数都是素数。
素数筛跑出1000到10000内的全部素数。假设当中两个素数仅仅有一位不同。那么连接一条边。得到全部素数组合的图后用bfs直接搜索就能够
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int a[11000] , check[11000] , tot ;
struct node{
int v , next ;
}p[2000000];
struct node1{
int u , t ;
};
queue <node1> que ;
int head[10000] , cnt , flag[10000] ;
void add(int u,int v)
{
p[cnt].v = v ;
p[cnt].next = head[u] ;
head[u] = cnt++ ;
}
void init()
{
memset(check,0,sizeof(check));
memset(head,-1,sizeof(head));
tot = cnt = 0 ;
int i , j , k , num ;
for(i = 2 ; i <= 10000 ; i++)
{
if( !check[i] )
a[tot++] = i ;
for(j = 0 ; j < tot ; j++)
{
if(i*a[j] >= 10000)
break;
check[i*a[j]] = 1 ;
if( i%a[j] == 0 )
break;
}
}
for(i = 0 ; i < tot ; i++)
if( (a[i]/1000) ) break;
k = i ;
for(i = k ; i < tot ; i++)
{
for(j = k ; j < i ; j++)
{
num = 0 ;
if( a[i]%10 != a[j]%10 )
num++ ;
if( a[i]/10%10 != a[j]/10%10 )
num++ ;
if( a[i]/100%10 != a[j]/100%10 )
num++ ;
if( a[i]/1000%10 != a[j]/1000%10 )
num++ ;
if(num == 1)
{
add(i,j);
add(j,i);
}
}
}
}
int find1(int x)
{
int low = 0 , mid , high = tot-1 ;
while(low <= high)
{
mid = (low+high)/2 ;
if(a[mid] == x)
return mid ;
else if(a[mid] < x)
low = mid + 1 ;
else
high = mid -1 ;
}
}
int bfs(int s,int e)
{
memset(flag,0,sizeof(flag));
while( !que.empty() )
que.pop();
int i , j , v ;
node1 low , high ;
low.u = s ;
low.t = 0 ;
flag[s] = 1 ;
que.push(low);
while( !que.empty() )
{
low = que.front();
que.pop();
if( low.u == e )
return low.t ;
for(i = head[low.u] ; i != -1 ; i = p[i].next)
{
v = p[i].v ;
if( !flag[v] )
{
flag[v] = 1;
high.u = v ;
high.t = low.t + 1 ;
que.push(high);
}
}
}
return 0;
}
int main()
{
int t , s , e ;
init();
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &s, &e);
s = find1(s);
e = find1(e);
printf("%d\n", bfs(s,e) );
}
return 0;
}
poj3126--Prime Path(广搜)的更多相关文章
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- POJ3126——Prime Path
非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path
http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
随机推荐
- scala学习5--函数二
to def test() : Unit = { // for(i <- 1.to(100)){ // println(i) // } for(i <- 1 to 100 ){ prin ...
- jmeter-----查看结果树
在编写接口测试脚本的时候,需要进行调试和查看结果是否正常的情况,这个时候可以使用查看结果树组件进行. 查看结果树中展示了每一个取样器的结果.请求信息和响应信息,可以查看这些内容去分析脚本是否存在问题. ...
- 如何在获取celery中的任务执行情况
开始以为在flower中获取,原来flower也是从celery中获取的. 如果直接用celery命令,一直会提示拒绝连接. 网上说了,用django命令就可以的. 于是试了下,OK了. 这样,至少可 ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- KVM调整cpu和内存
一.修改kvm虚拟机的配置 1.virsh edit centos7 找到“memory”和“vcpu”标签,将 <name>centos7</name> <uuid&g ...
- 百度地图API详解之自定义地图类型
http://blog.csdn.net/sup_heaven/article/details/8461586 今天的文章主要介绍如何利用地图API实现自定义地图. 百度地图API目前默认支持两种地图 ...
- react篇章-React 组件-ES6 class 来定义一个组件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- Mat矩阵(图像容器)创建及CV_8UC1、CV_8UC2等参数详解
CV_<bit_depth>(S|U|F)C<number_of_channels> 1--bit_depth---比特数---代表8bite,16bites,32bites, ...
- python 写一个贪吃蛇游戏
#!usr/bin/python #-*- coding:utf-8 -*- import random import curses s = curses.initscr() curses.curs_ ...
- Ubuntu系统 安装谷歌 Chrome 浏览器
在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器,步骤: 1.sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P / ...