非常水的一道广搜题(专业刷水题)。

。。

#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的更多相关文章

  1. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  2. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  3. poj3126 Prime Path(c语言)

    Prime Path   Description The ministers of the cabinet were quite upset by the message from the Chief ...

  4. POJ3126 Prime Path —— BFS + 素数表

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. POJ3126 Prime Path

    http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...

  6. POJ3126 Prime Path(BFS)

    题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  7. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  8. Prime Path POJ-3126

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  9. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

随机推荐

  1. JavaScript表格搜索高亮功能模拟

    在网页表格中模拟excle的搜索高亮显示功能.当在搜索框中输入需要的姓名时,若表格中存在对应的数据,则该表格背景色变为黄色. 下面为表的HTML源码: <!doctype html> &l ...

  2. NoSQL概念

    NoSQL是非关系型数据库,即not only sql,key/value键值对存储. 现有Nosql DB产品:Redis/MongoDB/Memcached等等. SQL Sever是关系型数据库 ...

  3. sql学习--update

    两种修改形式 第一种:静态插入 ,notes='began career selling ...balabala' where jc='johnny ca' 第二种: --注意别名和on后边的表连接不 ...

  4. P1796 汤姆斯的天堂梦_NOI导刊2010提高(05)

    题目描述 汤姆斯生活在一个等级为0的星球上.那里的环境极其恶劣,每天12小时的工作和成堆的垃圾让人忍无可忍.他向往着等级为N的星球上天堂般的生活. 有一些航班将人从低等级的星球送上高一级的星球,有时需 ...

  5. HTML简单入门

    - Java攻城狮学习路线 - 基本结构 标准文档:www.w3.org <!DOCTYPE html> <html> <head> <meta charse ...

  6. DeltaFish 校园物资共享平台 第五次小组会议

    软工第五次小组会议 记录人:娄雨禛 会议地点:三教讨论区 会议时间:9:00-10:00 与会人员:软工小组成员 一.前端会议提要 前端分为“2+2”组合进行开发. 底层设计:齐天扬,刘鼎乾 界面美化 ...

  7. Eclipse安装配置——For Java

    1.下载安装JRE 2.下载Eclipse,解压到相应文件夹 3.配置Eclipse 3.1 配置字体大小  -12号 3.2配置workspace默认编码,utf-8,默认系统windows 3.3 ...

  8. C# 获取正在使用的Mac地址

    /// <summary> /// 获得当前机器的活动中Mac地址,若无联网则返回空"" /// 需在项目引用中添加 System.Management /// < ...

  9. QT显示框架嵌入Vs控制台工程

      一.一些准备工作: 1.安装Qt for VS 的插件: 安装Qt for VS 的插件 下载地址:http://download.qt.io/official_releases/vsaddin/ ...

  10. 【sqli-labs】 less7 GET - Dump into outfile - String (导出文件GET字符型注入)

    使用此漏洞需要知道WEB程序的根路径并且MYSQL的secure-file-priv配置应为可写入该路径 添加单引号,发现错误被屏蔽 对应的SQL语句应为 ')) ... 字段还是3个 在数据库中执行 ...