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 ...
随机推荐
- [LeetCode] Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- 理解ASP.NET MVC的DependencyResolver组件
一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC容器.MVC内部很多对象的创建都是通过它完成的,或 ...
- Zend Framework 项目 index.php 的问题
默认生成的Zend项目在public目录下会自动生成一个.htaccess文件,这是用来实现伪静态,即隐藏index.php这个唯一入口文件的. 但是,搭建项目时遇到一个问题:URL中如果不加inde ...
- 关于GIT合并出错的记录
今天给美术解决GIT资源冲突时碰到的问题,搞了挺长时间终于解决了.参看下面这个网址:http://www.bujichong.com/m/68 今天git一小部分代码发现出错了, 上网查了一下, 大体 ...
- C语言学习 第九次作业总结
本次作业练习的内容是二维数组.下面我先简单的说下二维数组的基本知识点: 二维数组其实这个中文概念颇有误导--会让人感觉这是一个两个维度的概念.所以很多的国外的C语言书籍上会称这种数组为多下标数组:即首 ...
- laravel下的数据序列化
$data=$this->user->get(); //is obj $data=(string)$data; //is string $data=$data->toArray(); ...
- php curl获取的数据不直接输出
curl获取页面内容,不直接输出到页面 必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true curl_setopt($ch, CURLOPT_RETURNTRANSFER ...
- 建模前的数据清洗/ETL(python)
1. 读取数据 data= open('e:/java_ws/scalademo/data/sample_naive_bayes_data.txt' , 'r') 2. 把数据随机分割为trainin ...
- angular服务二
angular服务 $http 实现客户端与服务器端异步请求 get方式 test.html <!DOCTYPE html> <html lang="en"> ...