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 ...
随机推荐
- Aviator
Aviator 简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢 ...
- jsp页面导入excel文件的步骤及配置
上传使用flash插件 需要jquery.uploadify.min.js,uploadify.css,poi-ooxml-3.8-20120326.jar等 jsp页面: <%@include ...
- Unity引擎GUI之Image
UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...
- node 连接MySQL及其分装, 连接池连接
const mysql = require('mysql') const config = require('./../../config/config.default') var connectio ...
- PCL:描述三维离散点的ROPS特征(Code)
前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...
- javascript 基础知识点
NaN; // NaN表示Not a Number,当无法计算结果时用NaN表示 Infinity; // Infinity表示无限大,当数值超过了JavaScript的Number所能表示的最大值时 ...
- Apex语言(三)原始数据类型
1.原始数据类型(Primitive) 整数:Integer 双精度:Double 单精度:Decimal 长整型:Long 日期:Date 日期时间:Datetime 字符串:String ID:I ...
- Java接口和Java抽象类的认识
在没有好好地研习面向对象设计的设计模式之前,我对Java接口和Java抽象类的认识还是很模糊,很不可理解. 刚学Java语言时,就很难理解为什么要有接口这个概念,虽说是可以实现所谓的多继承,可一个只有 ...
- 团体程序设计天梯赛-练习集-L1-048. 矩阵A乘以B
L1-048. 矩阵A乘以B 给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两个矩阵 ...
- Restrictions.like("字段field","%表达式exp%");
Restrictions.like("字段field","%表达式exp%");用hql语句就是 from table where field like '%e ...