POJ 3126 Prime Path(素数打表+BFS)
来源:互联网 发布:什么是软件过程模型 编辑:程序博客网 时间:2024/06/11 19:54
题目链接:http://poj.org/problem?id=3126
题意:一个起点数,一个终点数,每次变换只能变换数的其中一位,并且只能是素数,问最少经过几次变换。
题解:最短路径BFS,素数预处理打表。注意第一位不能为0.
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int MAX=10000+10;const int INF=0x3f3f3f3f;int prim[MAX];int d[MAX];int a,b;int ans;void init(){memset(prim,0,sizeof(prim));for(int i=2;i<MAX;i++){if(!prim[i])for(int j=i*i;j<MAX;j+=i)prim[j]=1;}/*for(int i=1000;i<10000;i++){if(!prim[i]) cout<<i<<endl;}*/}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);init();int t;scanf("%d",&t);while(t--){queue<int> que;memset(d,0x3f,sizeof(d));scanf("%d%d",&a,&b);que.push(a);d[a]=0;while(!que.empty()){int top=que.front();que.pop();if(top==b){printf("%d\n",d[top]);}int tmp=1;for(int i=0;i<4;i++){int w=(top/tmp)%10;for(int j=w;j>0;j--){int na=top-j*tmp;if(!prim[na]&&d[na]==INF&&na>=1000){que.push(na);d[na]=d[top]+1;}}for(int j=w+1;j<=9;j++){int na=top+(j-w)*tmp;if(!prim[na]&&d[na]==INF&&na>=1000){que.push(na);d[na]=d[top]+1;}}tmp*=10;}}}return 0;}
0 0
- 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+素数打表)
- poj3262 Prime Path 素数打表+bfs
- POJ 3126 Prime Path (素数筛选+bfs)
- POJ 3126 Prime Path【素数+bfs】
- POJ 3126 Prime Path(BFS + 素数判定)
- POJ 3126 && HDU 1973 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最短路)
- F - Prime Path POJ 3126 筛选素数+bfs
- poj 3126 Prime Path (线性素数筛 + bfs)
- POJ 3126-Prime Path(BFS-素数变换一位)
- R语言入门总结
- hbase 安装指南
- 基础控件属性及方法整理 --> UIImageView
- 【C++】《C++标准程序库》小结第九章-算法
- 内存使用的管理方式
- POJ 3126 Prime Path(素数打表+BFS)
- 宏定义学习
- quartz集群
- 解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 问题。
- YTU 2481: 01字串
- 杭电2084数塔(逆序)
- Python中用pycurl监控http响应时间脚本分享
- Easyui 更改datagrid 字体大小
- Objective-C 协议 protocol