POJ 3126 Prime Path bfs, 水题 难度:0
题目
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的更多相关文章
- 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 + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- POJ 3126 Prime Path BFS搜索
题意:就是找最短的四位数素数路径 分析:然后BFS随便搜一下,复杂度最多是所有的四位素数的个数 #include<cstdio> #include<algorithm> #in ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- POJ 1840 Eqs 解方程式, 水题 难度:0
题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
随机推荐
- leecode第一百四十二题(环形链表II)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- Android 用虹软SDK做人脸识别
人脸识别第三方sdk比较多,但是大多都是收费的或者限制次数什么的,虹软的效果还不错,全免费也不需要联网 V1.2版本使用和快速集成:https://www.jianshu.com/p/8dee89ec ...
- 数据结构(C语言版)-第4章 串、数组和广义表
补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...
- English trip M1 - AC3 Teacher:Corrine
课堂上内容 16,black,games The clothes is Only $. is lucky number in China. God give us black eyes,but we ...
- Practical Node.js (2018版) 第3章:测试/Mocha.js, Chai.js, Expect.js
TDD and BDD for Node.js with Mocha TDD测试驱动开发.自动测试代码. BDD: behavior-driven development行为驱动开发,基于TDD.一种 ...
- Android 如何更改一个 imageview的颜色
xml中可以使用tint属性 tips:这里的图片必须是png格式的文件才行,不然你得到的就是一个被纯色覆盖的图 java中 使用 imageView.setColorFilter(Color.WHI ...
- 机器学习ML策略
1.为什么是ML策略 例如:识别cat分类器的识别率是90%,怎么进一步提高识别率呢? 想法: (1)收集更多数据 (2)收集更多的多样性训练样本 (3)使用梯度下降训练更长时间 (4)尝试Adam代 ...
- 使用scrapy-crawlSpider 爬取tencent 招聘
Tencent 招聘信息网站 创建项目 scrapy startproject Tencent 创建爬虫 scrapy genspider -t crawl tencent 1. 起始url sta ...
- Sphinx实时索引
数据库中的数据很大,然后我有些新的数据后来加入到数据库中,也希望能够检索到,全部重新建立索引很消耗资源,这样需要用到“主索引+增量索引”的思路来解决,这个模式实现的基本原理是设置两个数据源和两个索引. ...
- Cron 表达式详解(已整理、很清晰)
Cron表达式是一个字符串,字符串分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month DayofWeek ...