【Poj3126】【BNUOJ3245】Prime Path
http://poj.org/problem?id=3126
https://www.bnuoj.com/v3/problem_show.php?pid=3245
题目鬼图
刚开始看到题目的图觉得这题会很鬼,然后看题,发现是水题。。。
线性筛预处理素数→BFS+记录深度 or 迭代加深 or 分层BFS
// <3126.cpp> - 11/01/16 17:31:53
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int MAXN=10010;
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
queue<int>q;int f[MAXN],t[11];bool prime[MAXN];
int bfs(int u,int v){
if(u==v)return 0;
while(!q.empty())q.pop();
q.push(u);
memset(f,0,sizeof(f));
while(!q.empty()){
int x=q.front();q.pop();
t[0]=x/1000;t[1]=x%1000/100;t[2]=x%100/10;t[3]=x%10;
for(int i=0;i<4;i++){
int g=t[i];
for(int j=i==0?1:0;j<=9;j++)
if(j!=g){
t[i]=j;
int k=t[0]*1000+t[1]*100+t[2]*10+t[3];
if(!f[k]&&!prime[k]){
f[k]=f[x]+1;q.push(k);
if(k==v)return f[k];
}
}
t[i]=g;
}
}
return -1;
}
int main()
{
freopen("3126.in","r",stdin);
freopen("3126.out","w",stdout);
prime[1]=1;
for(int i=2;i<MAXN;i++)
if(!prime[i])
for(int j=i+i;j<MAXN;j+=i)prime[j]=1;
int t=gi();
while(t--){
int u=gi(),v=gi(),ans=bfs(u,v);
if(ans==-1)printf("Impossible\n");else printf("%d\n",ans);
}
return 0;
}
【Poj3126】【BNUOJ3245】Prime Path的更多相关文章
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- UVA P12101 【Prime Path】
题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#64. Minimum Path Sum.md
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...
- 【SVG】SVG的夺命利器——path
[SVG]SVG的夺命利器--path 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 昨天一发布,突然看到有朋友留言,希 ...
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
微信支付教程系列之公众号支付 今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...
随机推荐
- kvm:日常使用
常用命令 创建新镜像:qemu-img create -f qcow2 /var/lib/libvirt/images/$1.img 100G 创建新镜像:qemu-img create -f qco ...
- 在rubymine中集成heroku插件
先安装heroku,参见http://www.cnblogs.com/jecyhw/p/4906990.html Heroku安装之后,就自动安装上git,目录为C:\Program Files (x ...
- UI组件之色彩选择器
var myData = { canvas : document.getElementById('colors'),// context : myData.canvas.getContext('2d' ...
- 关于meta标签的使用,属性的说明
原文转自:http://blog.csdn.net/gavid0124/article/details/46826127 网页源代码中有时候会遇到这样的一段代码: <metaname=" ...
- 九度oj 题目1058:反序输出
题目1058:反序输出 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9677 解决:3495 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可 ...
- 全排列函数 nyoj 366(next_permutation()函数)
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- [SPOJ8222]Substrings
[SPOJ8222]Substrings 试题描述 You are given a string S which consists of 250000 lowercase latin letters ...
- navicat mysql 连接本地 忘记密码 查看密码 操作
https://jingyan.baidu.com/article/454316ab4e9e65f7a7c03ad1.html
- 网络编程基础:粘包现象、基于UDP协议的套接字
粘包现象: 如上篇博客中最后的示例,客户端有个 phone.recv(2014) , 当服务端发送给客户端的数据大于1024个字节时, 多于1024的数据就会残留在管道中,下次客户端再给服务端发命令时 ...
- fread了解一下
神奇读入挂^_^ 记得加头文件#include const int BufferSize=100*1000; char buffer[BufferSize],*head,*tail; bool not ...