【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 微信支付教程系列之公众号支付
微信支付教程系列之公众号支付 今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...
随机推荐
- org.dom4j.DocumentException
在使用soapui测试webservice接口的时候如果需要传入xml格式的参数 这么写是不对的,会报错org.dom4j.DocumentException: Error on line 1 of ...
- Go:冒泡排序
package main import "fmt" func BubbleSort(arr *[5]int) { fmt.Println("排序前:", *ar ...
- Python:webshell 跳板机审计服务器
1.修改paramiko源码包实现 https://github.com/paramiko/paramiko/tree/1.10.1 下载源码包 unzip paramiko-1.10.1.zip p ...
- 12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...
- atom 安装插件列表
插件列表 atom-beautify docblockr autocomplete-python goto-definition platformio-ide-terminal symbols-tre ...
- JavaScript:获取上传图片的base64
文章来源:http://www.cnblogs.com/hello-tl/p/7661535.html 1.HTML代码 <!DOCTYPE html> <html lang=&qu ...
- 【ZOJ - 3780】 Paint the Grid Again (拓扑排序)
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- use-gulp
参考: https://github.com/Platform-CUF/use-gulp use-gulp 为什么使用gulp? 首先看一篇文章 Gulp的目标是取代Grunt 根据gulp的文档,它 ...
- BNUOJ 5363 Machine Schedule
Machine Schedule Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...