POJ3126——Prime Path
非常水的一道广搜题(专业刷水题)。
。。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define inf 1000000
using namespace std;
int d[4]={1,10,100,1000};
int vis[10000];
int prime[10000];
int q[inf];
int n1,n2;
void isprime()
{
memset(prime,1,sizeof prime);
for(int i=2;i<10000;i++){
if(prime[i]){
for(int j=i*i;j<10000;j+=i){
prime[j]=0;
}
}
}
} void bfs()
{
int front=0,rear=0;
memset(q,0,sizeof(q));
memset(vis,0,sizeof( vis));
vis[n1]=1;
q[rear++]=n1;
while(front<rear){
int cur=q[front++];
for(int i=0;i<4;i++){
int t[4];
t[3]=cur/1000,t[2]=cur/100%10,t[1]=cur/10%10,t[0]=cur%10; for(int j=0;j<=9;j++){
if(j==t[i]) continue;
if(i==3&&j==0) continue;
else{
int next=(cur-t[i]*d[i])+j*d[i];
if(prime[next]&&!vis[next]){
if(next==n2){
cout<<vis[cur]<<endl;return;
}
else{
vis[next]=vis[cur]+1;
q[rear++]=next;
} }
}
}
} }
cout<<"0"<<endl;
} int main()
{
isprime();
int T;
cin>>T;
while(T--){
cin>>n1>>n2;
bfs();
}
return 0;
}
POJ3126——Prime Path的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path
http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
随机推荐
- 另一种压缩图片的方法---Machine learning 之 PCA(Principle Component Analysis)
PCA最主要的用途是用来减少特征向量的数目,N个特征向量 减小到 K个特征向量.如果为了可视化,k可以使3 或者 2.这样可以加速算法的学习速度. PCA用来压缩图像同一有效. 具体方式以及原理在gi ...
- SQLServer2008 关于Group by
如果我们想知道每个国家有多少种水果,那么我们可以通过如下SQL语句来完成: SELECT COUNT(*) FruitName AS 水果种类, ProductPlace AS 出产国 FROM T_ ...
- 单个句子<code> 多行代码显示<pre> 键盘输入<kbd>
1.用来显示单个句子或者单个单词:<code>……</code> 2.用来显示多行代码:<pre>……</pre> 当行数高度大于340px,自动出现y ...
- 大白话理解cookie
HTTP协议是一个无状态的协议,服务器无法区分出两次请求是否发送自同一服务器. 需要通过会话控制来解决这个问题,会话控制主要有两种方式Cookie 和 Session. Cookie就是一个头,Coo ...
- HTML 5概述
HTML语言是一种简易的文件交换标准,用于物理的文件结构,它旨在定义文件内的对象和描述文件的逻辑结构,而并不定义文件的显示.由于HTML所描述的文件具有极高的适应性,所以特别适合于WWW的出版环境. ...
- c++ 虚函数,纯虚函数的本质区别
转载博客:https://mp.weixin.qq.com/s?__biz=MzAxNzYzMTU0Ng==&mid=2651289202&idx=1&sn=431ffd1fa ...
- 分割字符串 ExtractStrings
//分割字符串 ExtractStrings var s: String; List: TStringList; begin s := 'about: #delphi; #pascal, progra ...
- python 上手
1.安装模块 cmd---“pip install [模块名]” 2.爬虫常用模块 requests beautifulsoup4 3.检查已安装的模块 cmd ---"pip list&q ...
- 去除安卓apk中的广告
一般来说,安卓应用很多免费的apk都是有广告的.尽管我们要坚持尊重开发者,帮帮他们点击广告赚钱来可持续发展,但是有的时候,很多游戏中游戏实在是太影响感觉了,当找不到汉化破解版本的时候,也许需要亲自把它 ...
- dubbo之线程模型
事件处理线程说明 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识,则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发起新的IO请求 ...