POJ 3126 Prime Path (BFS + 素数筛)
**链接 : ** Here!
**思路 : ** 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大...
/*************************************************************************
> File Name: E.cpp
> Author:
> Mail:
> Created Time: 2017年11月26日 星期日 10时51分05秒
************************************************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
#define MAX_N 100000
int isPrime[MAX_N] = {0}, primeList[MAX_N] = {0};
int T;
int vis[MAX_N];
struct info {
info() {}
info(int num, int step) : num(num), step(step) {}
int num, step;
};
info st, ed;
void init_prime() {
for (int i = 2 ; i < MAX_N ; ++i) {
if (!isPrime[i]) {
primeList[++primeList[0]] = i;
}
for (int j = 1 ; j <= primeList[0] ; ++j) {
if (i * primeList[j] >= MAX_N) break;
isPrime[i * primeList[j]] = 1;
if (i % primeList[j] == 0) break;
}
}
}
// 在num的第i个位置上替换为j
int transInfo(int i, int j, int num) {
int temp[4], k = 3, ret;
while (num) {
temp[k--] = num % 10;
num /= 10;
}
temp[i] = j;
return temp[0] * 1000 + temp[1] * 100 + temp[2] * 10 + temp[3];
}
int check(info p) {
if (p.num < 1000 || p.num >= 10000) return 0;
if (vis[p.num]) return 0;
if (isPrime[p.num]) return 0;
return 1;
}
int BFS() {
queue<info> que;
vis[st.num] = 1;
que.push(st);
while (!que.empty()) {
info now = que.front();
que.pop();
if (now.num == ed.num) {
return now.step;
}
for (int i = 0 ; i < 4 ; ++i) {
for (int j = 0 ; j < 10 ; ++j) {
// 最高位不能为0
if (i == 0 && j == 0) continue;
info temp(transInfo(i, j, now.num), now.step + 1);
if (!check(temp)) continue;
vis[temp.num] = 1;
que.push(temp);
}
}
}
return 0;
}
void solve() {
memset(vis, 0, sizeof(vis));
st.step = 0;
int ret = BFS();
printf("%d\n", ret);
}
int main() {
// freopen("./in.in", "r", stdin);
init_prime();
scanf("%d", &T);
while (T--) {
scanf("%d%d", &st.num, &ed.num);
solve();
}
return 0;
}
POJ 3126 Prime Path (BFS + 素数筛)的更多相关文章
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- POJ 3126 Prime Path (素数+BFS)
题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
随机推荐
- AutoCAD 2014:安装时发生allied product not found错误
有个朋友在安装AutoCAD 2014时不慎误删了一个文件夹,结果导致安装AutoCAD时总是跳出”allied product not found”的错误. Google搜了下,解决方案如下: 1. ...
- mysql无密码重启
mysql无密码重启 /etc/init.d/mysql stopnohup /usr/bin/mysqld_safe --user=mysql --skip-grant-tables &
- POJ 3748:位操作
位操作 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8964 Accepted: 3581 Description 如 ...
- android logo:内核、android开机动画【转】
本文转载自: 关键词:Android 开机logo 开机动画 initlogo.rle bootanimation desc.txt 平台信息:内核:linux2.6/linux3.0系统:a ...
- 倒排索引PForDelta压缩算法——基本假设和霍夫曼压缩同
PForDelta算法 PForDelta算法最早由Heman在2005年提出,它允许同时对整个chunk数据(例128个数)进行压缩处理.基础思想是对于一个chunk的数列(例128个),认为其中占 ...
- hdu 1754(单点更新 ,区间最大值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- JavaScript表格搜索高亮功能模拟
在网页表格中模拟excle的搜索高亮显示功能.当在搜索框中输入需要的姓名时,若表格中存在对应的数据,则该表格背景色变为黄色. 下面为表的HTML源码: <!doctype html> &l ...
- python 6:list.append(新元素)与list.insert(索引,新元素)(在列表末尾追加新元素、在索引处添加新元素)
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) bicycles.append("ho ...
- 2015 多校赛 第三场 1002 (hdu 5317)
Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...
- C#WebForm里面aspx,ajax请求后台。。。
虽然WebForm里面有那些基本控件,后台CS里面也有许许多多的控件的方法.但是不见得有些标签不需要进行后台的访问,下面介绍一下三种aspx中访问后台的方式.. 第一种:WebMethod (静态方法 ...