素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path
题目大意
从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数。求最终把m变成n所需的最少次数。
思路
BFS。搜索的时候,最低位为0,2,4,6,8可以先排除(偶数不会是素数),最高位是0的也可以排除。这个题判断素数的次数比较少,可以不打素数表。
题解
第二次写的时候代码写的很乱。。没有第一遍干净了
#include <iostream>
#include <cstring>
#include <queue>
using namespace std; int num, m, n, ans;
int vis[];
int a[] = {,,,,,,,,,};
int digit[];
struct point
{
int val;
int step;
}p; void div(int x) //把数拆分
{
for(int i = ; i < ; i++)
{
digit[i] = x % ;
x = x/;
}
} bool isPrime(int n)
{
for(int i = ; i*i <= n; i++)
{
if(n % i == )
{
return false;
}
}
return true;
} int bfs(int m, int n)
{
queue<point> q;
p.val = m;
p.step = ;
q.push(p);
while(!q.empty())
{
point cur = q.front();
if(cur.val == n)
{
return cur.step;
}
//cout << cur.val << " ";
vis[cur.val] = ;
div(cur.val);
for(int i = ; i < ; i++)
{
if(i == digit[] || i == || i == || i == || i == ) continue;
int x = digit[]* + digit[]* + digit[]* + i;
if(vis[x] || !isPrime(x)) continue;
p.val = x;
p.step = cur.step+;
q.push(p);
}
for(int i = ; i < ; i++)
{
if(i == digit[]) continue;
int x = digit[]* + digit[]* + i* + digit[];
if(vis[x] || !isPrime(x)) continue;
p.val = x;
p.step = cur.step+;
q.push(p);
} for(int i = ; i < ; i++)
{
if(i == digit[]) continue;
int x = digit[]* + i* + digit[]* + digit[];
if(vis[x] || !isPrime(x)) continue;
p.val = x;
p.step = cur.step+;
q.push(p);
}
for(int i = ; i < ; i++)
{
if(i == digit[]) continue;
int x = i* + digit[]* + digit[]* + digit[];
if(vis[x]|| !isPrime(x)) continue;
p.val = x;
p.step = cur.step+;
q.push(p);
}
q.pop();
}
return -; //没有找到
} int main(int argc, char const *argv[])
{
#ifdef debug
freopen("test.txt","r",stdin);
#endif
cin >> num;
while(num--)
{
memset(vis, ,sizeof(vis));
cin >> m >> n;
ans = bfs(m, n);
if (ans == -)
{
cout << "Impossible" << endl;
continue;
}
cout << ans << endl;
}
}
素数路径Prime Path POJ-3126 素数,BFS的更多相关文章
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
- kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126
题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...
- Mathematics:Prime Path(POJ 3126)
素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
- POJ 3126 primepath bfs
题目链接:http://poj.org/problem?id=3126 题意:1维的坐标轴,给出起点和终点,求从起点到终点变换经历的最短的步数.起点,终点和中间变换的数字都是4位,而且都是质数. 思路 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- Fortigate防火墙常用命令
命令结构 #config 对策略,对象等进行配置 #get 查看相关对象的参数 #show 查看配置文件 #diagnose 诊断命令 #execute 常用的工具命令,如ping treacer ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- mysql8.0版本下命令行mysqld –skip-grant-tables 失效,无法登陆的问题
1.管理员权限登陆cmd,不会使用管理员登陆的请搜索cmd,搜索结果右键. 2.命令行输入:net stop mysql;然后提示.服务停止中 --> 服务已停止,如出现其他错误请百度. 这只是 ...
- 第10章 文档对象模型DOM 10.1 Node节点类型
DOM是针对 HTML 和 XML 文档的一个 API(应用程序编程接口) .DOM描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分.DOM 脱胎于Netscape 及微软公司创始 ...
- chrome取消安全模式
右键快捷方式,后面添加--disable-web-security --user-data-dir=E:\MyChromeDevUserData "D:\Program Files (x86 ...
- HDU-10240Max Sum Plus Plus+动态规划+滚动数组
Max Sum Plus Plus 题意:题意理解了老半天,这里是说在给定数列中,取m组子数列,不能有重复,使得这些子序列的和最大: 就比如m=2时候,1 /2/-4/5/6.可以不用拿-4的意思: ...
- lightoj 1049 - One Way Roads(dfs)
Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...
- HDU 4565 So Easy! 广义斐波拉数 数论 (a+sqrt(b))^n%mod 模板
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- CodeForces Round #498 div3
A: 题目没读, 啥也不会的室友帮我写的. #include<bits/stdc++.h> using namespace std; #define Fopen freopen(" ...
- BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分+单点更新+区间求和+区间求最大值)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:略. 题解:树链剖分模版,注意一些细节即可. #include <ios ...