题目链接

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <ctype.h> using namespace std; const int maxn = ; struct node {
int x, s;
}; int end;
bool vis[maxn], prime[maxn]; int BFS(int s) { if(s == end) return ; queue<node> Q;
memset(vis, , sizeof(vis)); vis[s] = true;
Q.push((node){s, }); while(!Q.empty()) {
  node e = Q.front(); Q.pop();
  for(int i=; i<=; i *= ) {
   for(int j=; j<; j++) {
      if(i == && j == ) continue;
     int t = e.x%i+j*i+e.x/(i*)*(i*);
    if(t == end) return e.s+;
    if(prime[t]) {
      if(vis[t]) continue;
      vis[t] = true;
     Q.push((node){t, e.s+});
     }
  }
  }  
}
return -;
} int main() {
int T, s;
scanf("%d", &T); memset(prime, true, sizeof(prime));
prime[] = prime[] = false; for(int i=; i*i<maxn; i++) {
  if(prime[i])
    for(int j=i*i; j<maxn; j += i) {
     prime[j] = false;
    }
} while(T--) {
   scanf("%d%d", &s, &end);
  printf("%d\n", BFS(s));
} return ;
}

POJ3126 Prime Path(BFS)的更多相关文章

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

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

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

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

  3. poj3126 Prime Path 广搜bfs

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

  4. POJ2126——Prime Path(BFS)

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

  5. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  6. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  7. poj 3126 Prime Path bfs

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

  8. poj3126 Prime Path(c语言)

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

  9. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

随机推荐

  1. docker入门(一)

    docker安装 yum install -y docker-io [root@centos ~]# yum install -y docker-io 已加载插件:fastestmirror, lan ...

  2. mysql 操作指令笔记

    设置区分大小写: 打开my.ini,最后加入: [mysqld] lower_case_table_names=2 (2表示区分大小写,但仅限于字段,数据库名.表名.存储过程名都是小写的) 查看方法: ...

  3. 客户端session与服务端session

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  4. java验证码Captcha

    import java.awt.Color; import java.io.IOException; import java.util.Random; import javax.servlet.htt ...

  5. 解决dispaly:inline-block 遗留间隙的问题

    今天做一个项目 .本来我打算是作成表格的 ,后来觉得太费事直接搞成一个div 里面直接放四个a ,然后我将a 设置成inline-block.刚开始还没发现任何间隙问题,(对了说到这里 博主给新手介绍 ...

  6. C#中的Dictionary字典类介绍

      Dictionary字典类介绍 必须包含名空间System.Collection.Generic    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)    键必须是 ...

  7. 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本

    ng.module('index-filters', []) .filter('trustHtml', function ($sce) { return function (input) { retu ...

  8. 【转】iOS-Core-Animation-Advanced-Techniques(一)

     图层树.寄宿图以及图层几何学(一)图层的树状结构 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 原文:http://www.cocoachina.com/ios/20150104/1 ...

  9. AFNETWORKING tabelView没有reloadData,报错unsupported URL

    Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x7f9dc278aa60 {NSUnde ...

  10. JavaScript_变量的自动转换和语句

    JS自动类型转换 var a = 1; var b = true; "==" 表示 可以自动类型转换,比较的是数值 "===" 表示可以自动类型转换,先比较数值 ...