【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 微信支付教程系列之公众号支付
微信支付教程系列之公众号支付 今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...
随机推荐
- 笔试算法题(04):实现 string & memcpy & strcpy & strlen
出题:请实现给定String的类定义: 分析:注意检查标准类构造注意事项: 解题: #include <stdio.h> #include <string.h> /** * 检 ...
- Springboot开启事务
参考资料: https://blog.csdn.net/message_lx/article/details/77584847
- springData Jpa 快速入门
前言: 数据持久化的操作,一般都要由我们自己一步步的去编程实现,mybatis通过我们编写xml实现,hibernate也要配置对应的xml然后通过创建session执行crud操作.那么有没有这样一 ...
- memcache适用和不适用场景[转载]
适用memcached的业务场景:1)如果网站包含了访问量很大的动态网页,因而数据库的负载将会很高.由于大部分数据库请求都是读操作,那么memcached可以显著地减小数据库负载.2)如果数据库服务器 ...
- linux shell 获得当前程序的路径
filepath=$(cd "$(dirname "$0")"; pwd) 脚本文件的绝对路径存在了环境变量filepath中,可以用 echo $filepa ...
- Codeforces 631A Interview【模拟水题】
题意: 模拟模拟~~ 代码: #include<iostream> using namespace std; const int maxn = 1005; int a[maxn], b[m ...
- 创建简单的spring-mvc项目
1.第一步:创建项目 new—>Dynamic Web Project 项目创建成功后,展示如图: 2.第二步:导入springmvc的jar包和common-logging.jar 3.第三步 ...
- JDBC基础教程:tutorialspoint-jdbc
来自turorialspoint的JDBC基础教程(英文),官网:https://www.tutorialspoint.com/jdbc/index.htm 这个教程在国内已经被翻译成中文(不过是属于 ...
- TensorFlow-GPU环境配置之四——配置和编译TensorFlow
首先,使用configure进行配置 配置完成后,使用bazel编译retrain命令,编译命令中加入--config=cuda即为启用GPU 编译进行中... 编译完成 编译完成后,调用retrai ...
- Windows TAKEOWN & ICACLS
TAKEOWN:takeown /f 文件名 获取该文件的所属权takeown /f /r /d n 文件夹 获取整个文件夹及其下面子目录文件的所属权 TAK ...