POJ3126 Prime Path

  一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径,

而且代码实现起来特别麻烦,后来搜了一下解题报告,才发现是bfs().

  想想也是,题目其实已经提示的很清楚了,求最短的路径,对于每一个数,每次可以把4位中的任意一位,  换成与该位不相同的0-9中的任意一位,对于迷宫类

bfs每次转移数为上下左右四个状态,而此题就相当于每次可以转移40个状态(其实最低位为偶数可以排除,不过题目数据量较小,就不剪枝了),每次转移的这40个状态,都可能得到最小路径,对于每次转移得到的素数,标记一下used,同时标记一下深度(即所走过的步数)root,从起点m出发,每次进行40个方向的状态转移,直至找到终点n或者队列取空返回无解(-1);

    一开始把rep(j,0,10)写成了rep(j,0,9),wa了半天,这种小细节一定要注意.

/*
* Created: 2016年03月30日 08时06分49秒 星期三
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; bool used[];
int root[];
int s[];
bool prime(int n){ //素数测试
for(int i=;i*i<=n;i++){
if(n%i==) return false;
}
return n!=; //1是例外
}
int bfs(int m,int n){ //m相当于起点,n相当于终点,此题就转化为求每次可以走40个方向的迷宫问题的最短距离;
queue <int> que;
que.push(m);
int q,now;
while(!que.empty()){
q=que.front();
que.pop();
if(q==n) return root[q];
s[]=q/;s[]=(q/)%;
s[]=(q%)/;s[]=q%;
rep(i,,){ //每次改变4位数中的一位
rep(j,,){ //j>=0&&j<=9,此处不能写成rep(j,0,9);
if(j!=s[i]){
now=q-(s[i]-j)*pow(,i);
if((!used[now])&&(prime(now))&&(now>)){ //注意最高为不能是0
root[now]=root[q]+;//步数加1
used[now]=true; //标记该素数已被使用
que.push(now);
}
}
}
}
}
return -; //从起点m出发所有能够转移到的素数都已用完,没找到解,就返回-1
}
int main()
{
int t,a,b;
in(t);
while(t--){
in2(a,b);
fill(used,used+,false);
mem(root,);
int ans=bfs(a,b);
if(ans!=-) out(ans);
else printf("Impossible");
}
return ;
}

POJ3126 Prime Path (bfs+素数判断)的更多相关文章

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

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

  2. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

  3. POJ3126 Prime Path(BFS)

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

  4. POJ 3126 Prime Path (BFS + 素数筛)

    链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...

  5. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  6. poj3126 Prime Path 广搜bfs

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

  7. POJ 3126:Prime Path(素数+BFS)

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

  8. CD0J/POJ 851/3126 方老师与素数/Prime Path BFS

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 Descri ...

  9. POJ2126——Prime Path(BFS)

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

随机推荐

  1. 使用 InstallShield limited edition 打包部署Outlook 2013 Office add-in插件

    原文: Outlook: Deploying an Outlook 2013 add-in (using InstallShield LE) Today I had to create an inst ...

  2. 【Linux】鸟哥的Linux私房菜基础学习篇整理(八)

    1. useradd [-u UID] [-g 初始用户组] [-G 次要用户组] [-mM]\   [-c 说明栏] [-d 主文件夹绝对路径] [-r] [-s shell] 用户账号名:新增用户 ...

  3. swiper 多个循环的实现

    swiper 最好要一一对应,最好与id关联. new Swiper('#guess .swiper-container', { pagination: '#guess .swiper-paginat ...

  4. MongoDB ‘conn’Mongo 对象远程代码执行漏洞

    漏洞名称: MongoDB ‘conn’Mongo 对象远程代码执行漏洞 CNNVD编号: CNNVD-201307-497 发布时间: 2013-07-25 更新时间: 2013-07-25 危害等 ...

  5. hdu-3046-Pleasant sheep and big big wolf(最大流最小割)

    题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...

  6. 在线LCA模板

    在线LCA 如求A,B两点的LCA,先计算出各个结点的深度d[],然后,通过递推公式求出各个结点的2次方倍的祖先p[],假设d[A] > d[B],则找到d[p[A][i]] == d[B]也就 ...

  7. ♫【jQuery】detach

    Jquery empty() remove() detach() 方法的区别 <!DOCTYPE html> <html> <head> <meta char ...

  8. Xcode7真机调试iOS应用程序

    金田 近日苹果发布的新的Xcode7带来了许多特性,比如:swift语言比以前运行更快.功能更强.代码具有更高的可读性.Xcode的测试功能可以帮助用户记录应用程序的行为等,还有我们今天要讲到的Xco ...

  9. 图论(二分图,KM算法):HDU 3488 Tour

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  10. Jenkins 学习资料

    学习资料: iTech's Blog: Jenkins 入门总结 爱自己: 18篇博客 阳光温暖了心情: 17 篇博客 官网 参考: 构建基于Jenkins + Github的持续集成环境 CI持续集 ...