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 ...
随机推荐
- Ubuntu下git的安装与使用
Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...
- sql server 公共表达式的简单应用(cte)
一.前言 现在做项目数据访问基本都会选择一种orm框架,它以面向对象的形式屏蔽底层的数据访问形式,让开发人员更集中在业务处理上,而不是和数据库的交互上,帮助我们提高开发效率:例如一些简单的insert ...
- Webwork 学习之路【08】结合实战简析Controller 配置
虽然现在 MVC 框架层出不穷,但做为 Struts 前身的 webwork. 其经典程度不亚于贝利之于足球,双 11 之于淘宝特卖. 本篇将结合 webwork controller 配置文件 xw ...
- underscore 源码解读之 bind 方法的实现
自从进入七月以来,我的 underscore 源码解读系列 更新缓慢,再这样下去,今年更完的目标似乎要落空,赶紧写一篇压压惊. 前文 跟大家简单介绍了下 ES5 中的 bind 方法以及使用场景(没读 ...
- MongoDB的备份和恢复
1.导出数据库/备份: @echo off F: cd F:\software1\mongdb\mongodb-win32-x86_64-\bin start mongodump.exe -h -d ...
- Android样式和主题
样式:style--> 主题:theme--> <style name="my_style"> <item name="android:te ...
- git 指令
單獨看這個 commit id 做了什麼修改 git show CommitID or git diff CommitID^!
- php 设计模式--准备篇
要了解设计模式 首先我们要先了解 php的命名空间和类的自动载入的功能 下面我们来说一下 命名空间 概念缘由:比如一个a.php的文章 但是我们需要两个 此时同一个目录下不可能存在两个a.php 那么 ...
- 【BZOJ 3876】【AHOI 2014】支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 这道题每条支线的意思是每条边... 那么每条边的下界设为1就行了. 这样建出一个DAG,每条边 ...
- Azure IaaS限制
每个云服务最多运行 50 个虚机 每个云服务最多有 150 个输入 endpoints