题目

http://poj.org/problem?id=3126

题意

多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数。

思路

质数明显需要先处理出来,然后采用bfs获取结果即可。

感想

下次需要先计算空间复杂度, 1e8的空间复杂度肯定会MLE

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 10000; bool p[maxn];
int ans[maxn];
int b[9 * 4], num; void calPrime()
{
p[2] = true;
for(int i = 3; i < maxn; i += 2)p[i] = true;
for(int i = 3; i < maxn; i += 2)
{
if(p[i])
{
for(int j = 3; j * i < maxn; j+= 2)
{
p[i * j] = false;
}
}
}
} void getSameShape(int s)
{
num = 0;
for(int base = 1; base < maxn; base *= 10)
{
int t = s - ((s % (base * 10)) / base) * base;
for(int i = 0; i < 10; i++)
{
int tmp = t + i * base;
if(tmp > 1000 && p[tmp] && tmp != s)
{
b[num++] = tmp;
}
}
}
} void calAns(int s, int e)
{
memset(ans, -1, sizeof ans);
if(!p[s])return;
ans[s] = 0;
queue<int> que;
que.push(s);
while(!que.empty())
{
s = que.front();
que.pop();
getSameShape(s);
for(int j = 0; j < num; j++)
{
if(ans[b[j]] == -1)
{
ans[b[j]] = ans[s] + 1;
if(e == b[j])return;
que.push(b[j]);
}
} }
} int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
int n, m;
int T;
calPrime();
scanf("%d",&T);
for(int ti = 0; ti < T && scanf("%d%d", &n, &m) == 2; ti++)
{
calAns(n, m);
if(ans[m] != -1)printf("%d\n",ans[m]);
else puts("Impossible");
}
return 0;
}

POJ 3126 Prime Path bfs, 水题 难度:0的更多相关文章

  1. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  3. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  4. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  5. POJ 3126 Prime Path BFS搜索

    题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...

  6. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

  7. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  8. POJ 1840 Eqs 解方程式, 水题 难度:0

    题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...

  9. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

随机推荐

  1. ZOJ 3962 Seven Segment Display

    Seven Segment Display 思路: 经典数位dp 代码: #include<bits/stdc++.h> using namespace std; #define LL l ...

  2. Python中read()、readline()和readlines()三者间的区别和用法

    2019-01-15 10:48:43 前言 众所周知在python中读取文件常用的三种方法:read(),readline(),readlines(),今天看项目是又忘记他们的区别了.以前看书的时候 ...

  3. Mysql数据库如何自动备份

    Mysql数据库如何自动备份 一.总结 一句话总结:用navicat配合windows的批处理即可 navicat windows批处理 二.Mysql数据库自动备份 参考:Mysql数据库自动备份 ...

  4. 使用getCurrentPosition方法实时获取当前Geolocation信息(附源码文件)--html5、JavaScript

    使用getCurrentPosition方法实时获取当前Geolocation信息: 1.getCurrentPosition方法的使用 navigator.geolocation.getCurren ...

  5. 安装 zookeeper cluster

    安装 zookeeper cluster zookeeper-3.4.5.tar.gz n0 10.69.10.193 n1 10.69.10.188 n2 10.69.10.192 n0 上解压 z ...

  6. mysql防注入

    1.对用户输入的数据进行过滤 2.永远不要使用动态拼装sql,可以使用参数化的sql或者直接使用存储过程进行数据查询存取. 3.永远不要使用管理员权限的数据库连接,为每个应用使用单独的权限有限的数据库 ...

  7. python记录_day04(补充)

    列表删除 切片删除 del lis[1:3]  # 删除索引是1,2的元素 修改 索引切片修改 # 修改 lst = ["太白", "太", "五&q ...

  8. python low版线程池

    1.low版线程池设计思路:运用队列queue 将线程类名放入队列中,执行一个就拿一个出来import queueimport threading class ThreadPool(object): ...

  9. 7. 反转整数(Reverse Integer) C++

    知识点: 内置变量 INT_MAX   INT_MIN 运算结果是否溢出的判断 判断pop>7即pop>INT_MAX%10 判断pop<-8即pop<INT_MIN%10 c ...

  10. 「jQuery」获取元素的高度

    在jQuery中,获取元素高度的方法有3个:height().innerHeight().outerHeight(); 顺带记一下元素的盒模型: height(高度), padding(内边距), m ...