POJ 3126 Prime Path (BFS)
来源:互联网 发布:c语言幻数什么意思 编辑:程序博客网 时间:2024/05/21 14:08
大致题意:
给定两个四位素数a b,要求把a变换到b
变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
求从a到b最少需要的变换次数。无法变换则输出Impossible
解题思路:
四个数拿来模拟一下就好了。。注意千位非零!!!!!!!
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<string>using namespace std;const int N = 1e4 + 100;int vis[N],f[5],b[5];struct P{ int num,step; P(int num,int step):num(num),step(step){} P(){}};bool work(int num){ int i; for(i=2;i<=sqrt(num);i++) { if(num%i==0) return true; } return false;}int n,m;queue<P>que;int bfs(){ int i,j,k; memset(vis,0,sizeof(vis)); while(!que.empty()) que.pop(); P p = P(n,0); vis[n]=1; que.push(p); while(!que.empty()) { p = que.front(); que.pop(); if(p.num==m) { return p.step; } int num = p.num; int cnt=0; while(num) { b[++cnt]=num%10; num/=10; } for(i=1;i<=4;i++) { for(j=0;j<10;j++) { if(i==4 && j==0) continue; num=f[i]*j; for(k=1;k<=4;k++) { if(k==i) continue; num+=b[k]*f[k]; } if(vis[num] || work(num)) continue; vis[num]=1; que.push(P(num,p.step+1)); } } }}int main(){ int i,j,T; f[1]=1; for(i=2;i<=4;i++) { f[i]=f[i-1]*10; } scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); printf("%d\n",bfs()); } return 0;}
0 0
- poj poj 3126 Prime Path(BFS)
- poj 3126 Prime Path(bfs搜索)
- POJ 3126 Prime Path (BFS)
- poj 3126 Prime Path(BFS)
- poj 3126 Prime Path (bfs)
- POJ 3126 Prime Path(BFS算法)
- Prime Path (poj 3126 bfs)
- POJ 3126 Prime Path(BFS)
- POJ 3126 - Prime Path(BFS)
- poj 3126 Prime Path(BFS)
- POJ 3126 Prime Path(BFS)
- POJ 3126 Prime Path (bfs、埃氏筛法)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path(bfs)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path(BFS)
- git的使用
- 10 个有趣的 Linux 命令行小技巧
- HDU1856 More is better
- Retrofit使用总结
- 语音学习笔记13------谈谈斯坦福大学卷积神经网络之反向传播
- POJ 3126 Prime Path (BFS)
- CentOS环境下elasticsearch集群搭建
- javascript基础
- 周六日常训练,背包dp,树形dp,简单dp以及很多数学?
- Android视图状态及重绘流程分析,带你一步步深入了解View(三)
- Swift 3 编程语言
- 对url字符串中域名的三种截取方式
- WebStorm基本使用
- java中方法的参数传递机制