【POJ】3126
来源:互联网 发布:淘宝男鞋店铺推荐知乎 编辑:程序博客网 时间:2024/06/16 18:14
http://poj.org/problem?id=3126
给定两个四位素数a 、 b,要求把a变换到b
每次只能变一个数,且变的这个数是素数,求最少变换次数。
素数表+bfs
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace std;int n;int prime[10005];bool is[10005];int sieve(){ int sum=0; memset(is,true,sizeof(is)); is[0]=is[1]=false; for (int i=2;i<=10000;i++){ if (is[i]){ prime[sum++]=i; for (int j=i*2;j<=10000;j+=i){ is[j]=false; } } } return sum;}int get_next(int number,int digit,int change){ switch(digit){ case 0: return number/10*10+change; case 1: return number/100*100+number%10+change*10; case 2: return number/1000*1000+number%100+change*100; case 3: return number%1000 +change*1000; } return 0;}int f[10005];int main(){ sieve(); cin >> n; while (n--){ int from,to; cin >> from >> to; memset(f,0x3f,sizeof(f)); f[from]=0; queue <int> q; q.push(from); while (!q.empty()){ int now=q.front(); q.pop(); for (int i=0;i<4;i++){ for (int j=0;j<10;j++){ if (i==3&&j==0) continue; int next=get_next(now,i,j); if (!is[next]||f[next]<=f[now]) continue; f[next]=f[now]+1; q.push(next); } } } cout << f[to] << endl; }}
阅读全文
0 0
- POJ 3126
- poj 3126
- POJ 3126
- POJ 3126
- poj 3126
- poj 3126
- poj 3126
- POJ 3126
- poj 3126
- poj-3126
- poj 3126
- POJ 3126
- poj 3126
- 3126POJ
- poj 3126
- POJ 3126
- poj---3126
- poj 3126
- 目标跟踪算法——DSST(Discriminative Scale Space Tracker)
- BZOJ 3261 最大异或和 && qwb VS 去污棒(可持久化01Trie)
- 谷歌的gson解析
- Unity入门操作_UGUI_ScrollBar_029
- CodeForces 487 B.Strip(dp+尺取+set)
- 【POJ】3126
- 从程序员到CTO的Java技术路线图
- Clickhouse入门
- Spark源码系列(一)spark-submit提交作业过程
- 数组的首地址
- AI开发实战10-文件管理器(File)的定制
- HDOJ 1325 Is It A Tree(并查集)
- C#反射
- 04 JS的函数