Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A、B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B。可以直接进行BFS搜索
- #include<bits/stdc++.h>
- using namespace std;
- bool isPrime(int n){//素数判断
- if(n == || n == ) return true;
- else{
- int k = sqrt(n) + ;
- for(int i = ; i < k; i++){
- if(n % i == ) return false;
- }
- return true;
- }
- }
- bool Prime[];
- int visit[];
- void getPrime(){
- for(int i = ; i < ; i++){
- if(isPrime(i))Prime[i] = true;
- }
- }
- struct Node{
- int num;//储存数字
- int cost;//操作步数
- };
- Node bfs(int start, int end){
- queue<Node> q;
- Node front;
- front.num = start;
- front.cost = ;
- visit[start] = ;
- q.push(front);
- while(!q.empty()){
- front = q.front(); q.pop();
- for(int i = ; i < ; i++){//换千位
- int m = front.num;
- m = m % + i * ;
- if(!visit[m] && Prime[m]){
- visit[m] = ;
- Node tmp = front;
- tmp.num = m;
- tmp.cost++;
- q.push(tmp);
- if(m == end)return tmp;
- }
- }
- for(int i = ; i < ; i++){//换百位
- int m = front.num;
- m = m % + (m/) * + i * ;
- if(!visit[m] && Prime[m]){
- visit[m] = ;
- Node tmp = front;
- tmp.num = m;
- tmp.cost++;
- q.push(tmp);
- if(m == end)return tmp;
- }
- }
- for(int i = ; i < ; i++){//换十位
- int m = front.num;
- m = m % + (m/) * + i * ;
- if(!visit[m] && Prime[m]){
- visit[m] = ;
- Node tmp = front;
- tmp.num = m;
- tmp.cost++;
- q.push(tmp);
- if(m == end)return tmp;
- }
- }
- for(int i = ; i < ; i++){//换个位
- int m = front.num;
- m = (m/) * + i;
- if(!visit[m] && Prime[m]){
- visit[m] = ;
- Node tmp = front;
- tmp.num = m;
- tmp.cost++;
- q.push(tmp);
- if(m == end)return tmp;
- }
- }
- }
- Node tmp;
- tmp.num = ;
- tmp.cost = ;
- return tmp;
- }
- int main(){
- int n;
- cin >> n;
- getPrime();
- while(n--){
- int a, b;
- cin >> a >> b;
- Node tmp;
- memset(visit, , sizeof(visit));
- tmp = bfs(a, b);
- if(tmp.num == && tmp.cost == ) cout << << endl;
- else{
- cout << tmp.cost << endl;
- }
- }
- }
Sicily 1444: Prime Path(BFS)的更多相关文章
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- poj3216 Prime Path(BFS)
题目传送门 Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- POJ-3126-Prime Path(BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27852 Accepted: 15204 Desc ...
随机推荐
- Basic EEG waves 四种常见EEG波形
Source: https://www.medicine.mcgill.ca/physio/vlab/biomed_signals/eeg_n.htm The electroencephalogram ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- WPF简单模拟QQ登录背景动画
介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- Mybatis关联查询和数据库不一致问题分析与解决
Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...
- 理解OAuth 2.0
转自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...
- [转]HQL中的子查询
原文地址:http://blog.csdn.net/xb12369/article/details/8638683 子查询: 子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一 ...
- Markdown
1. 斜体和粗体 代码: *斜体*或_斜体_ **粗体** ***加粗斜体*** ~~删除线~~ 显示效果: 这是一段斜体 这是一段粗体 这是一段加粗斜体 这是一段删除线 2. 分级标题 第一种写法: ...
- PHP 做文件校验,MD5,CRC32,SHA等
函数 hash_file(): 使用给定文件的内容生成哈希值 说明 string hash_file ( string $algo , string $filename [, bool $raw_ou ...