素数路(prime) (BFS)
问题 C: 素数路(prime)
时间限制: 1 Sec 内存限制: 64 MB
提交: 8 解决: 5
[提交][状态][讨论版]
题目描述
已知一个四位的素数,要求每次修改其中的一位,并且要保证修改的结果还是一个素数,还不能出现前导零。你要找到一个修改数最少的方案,得到我们所需要的素数。
例如把1033变到8179,这里是一个最短的方案:
1033
1733
3733
3739
3779
8779
8179
修改了6次。
输入
1行,两个四位的素数(没有前导零),表示初始数和目标数。
输出
一个数,表示最少的操作次数。如果不可能,输出“Impossible”。
样例输入
1033 8179
样例输出
6
【水题】还是简单的BFS一下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
int n,dp[N],len,m;
int w[][];
int g[];
int flag=;
int vis[]={};
string str[],ch;
int maxn=;
map<string,int>p,pp;
struct man
{
int x,step;
};
int charge(int y)
{
for(int ii=;ii<=sqrt(y);ii++)
{
if(y%ii==)return ;
}
return ;
}
void bfs()
{
queue<man>q;
man s;s.step=;s.x=n;
q.push(s);
vis[n]=;
while(!q.empty())
{
man t=q.front();
q.pop();
//printf("%d %d\n",t.x,t.step);
if(t.x==m){flag=;
cout<<t.step<<endl;return;
}
int a[];a[]=t.x/;a[]=t.x/%;a[]=t.x/%;a[]=t.x%;
for(int i=;i<;i++)
{
for(int j=;j<=;j++)
{
if(i==&&j==)continue;
int k=t.x-a[i]*pow(,i-)+j*pow(,i-);
//printf("%d %d\n",k,charge(k));
if(charge(k)==&&vis[k]==)
{
vis[k]=;
man K;
K.x=k;K.step=t.step+;
q.push(K);
}
}
}
}
} int main() {
memset(w,,sizeof(w));
cin>>n>>m;
bfs();
if(flag==)puts("Impossible");
return ;
}
素数路(prime) (BFS)的更多相关文章
- 素数路(prime)
素数路(prime) 题目描述 已知一个四位的素数,要求每次修改其中的一位,并且要保证修改的结果还是一个素数,还不能出现前导零.你要找到一个修改数最少的方案,得到我们所需要的素数. 例如把1033变到 ...
- Prime Path素数筛与BFS动态规划
埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- POJ-3126.PrimePath(欧拉筛素数打表 + BFS)
给出一篇有关素数线性筛和区间筛的博客,有兴趣的读者可以自取. 本题大意: 给定两个四位的素数,没有前导零,每次变换其中的一位,最终使得两个素数相等,输出最小变换次数.要求变换过程中的数也都是素数. 本 ...
- UVa 1644 (筛素数 + 二分) Prime Gap
题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...
- 超级素数(sprime) (BFS)
问题 G: 超级素数(sprime) 时间限制: 1 Sec 内存限制: 64 MB提交: 47 解决: 11[提交][状态][讨论版] 题目描述 超级素数是指一个素数,每去掉后面一个数字,总能保 ...
- 素数(Prime)
素数的判断: #include<math.h> bool IsPrime(int n) { ) return false; int sqr = (int)sqrt(1.0*n); ; i& ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- 洛谷 P2501 [HAOI2006]数字序列 解题报告
P2501 [HAOI2006]数字序列 题目描述 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大. ...
- 在linux环境下让java代码生效的步骤
1.kill jboss 2.compile 3.deploy 4.bootstrap jboss.
- ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证
控制端安全认证: ActiveMQ目录conf下jetty.xml: <bean id="securityLoginService" class="org.ecli ...
- javascript继承有5种实现方式
1.对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(this.user ...
- HTTP协议中的ContenType类型大全(转)
".*"="application/octet-stream"".001"="application/x-001"&qu ...
- [POI2014] KUR-Couriers(洛谷P3567)
洛谷题目链接:[POI2014]KUR-Couriers 题目描述 Byteasar works for the BAJ company, which sells computer games. Th ...
- Go 实现 soundex 算法
[转]http://www.syyong.com/Go/Go-implements-the-soundex-algorithm.html SOUNDEX 返回由四个字符组成的代码 (SOUNDEX) ...
- ecma 2018, javascript spread syntax behaves like Object.assign
as the subject. It is only supported in Chrome version 60+, so, first check the version, or just use ...
- (12)Linux shell之read 用法
Linux shell之read 用法 #!/bin/bash#read 用来读取屏幕输入或是读取文件内容.read -p "please input you name: " ...
- 两个kernel.org国内镜像
两个kernel.org国内镜像 https://mirror.tuna.tsinghua.edu.cn/kernel/v4.x/testing/ http://mirror.bjtu.edu.cn/ ...