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 ...
随机推荐
- Django基础,Day1 - 环境安装与pycharm创建django项目
Django是一个高级Python Web框架,支持快速部署,清理和实用的设计.它可以被轻易部署和提供实用的组件,而开发人员只需要专注于写自己的应用程序,而不需要重复造轮子.并且Django是自由和开 ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- c#与JavaScript实现对用户名、密码进行RSA非对称加密
博主最近手上这个项目呢(就是有上百个万恶的复杂excel需要解析的那个项目,参见博客:http://www.cnblogs.com/csqb-511612371/p/4885930.html),由于是 ...
- .NET跨平台之旅:将示例站点升级至ASP.NET Core 1.0
北京时间6月28日凌晨,微软发布了 .NET Core 1.0,详见新闻 .NET Core 1.0 正式发布了 ,ASP.NET Core 1.0 也随之一起发布了. 紧跟这次发布,我们将跑在 Li ...
- Ubuntu下基于Saprk安装Zeppelin
前言 Apache Zeppelin是一款基于web的notebook(类似于ipython的notebook),支持交互式地数据分析,即一个Web笔记形式的交互式数据查询分析工具,可以在线用scal ...
- 阻止pc端浏览器缩放js代码
阻止pc端浏览器缩放js代码 众所周知:移动端页面禁止用户缩放界面只需加上<meta name="viewport" content="user-scalable= ...
- C#-WebForm-Repeater-重复器
Repeater-重复器 - 类似WinForm中的ListView,用列表来展示数据 格式: <body> <form id="form1" runat=&qu ...
- angularjs $emit $on $broadcast 父子 兄弟之间传值
父子之间 <div ng-controller="ParentCtrl"> <div ng-controller="ChildCtrl"> ...
- Shiro-集成Spring
集成Spring 加入Spring 和Shiro的jar 包 配置Spring 及SpringMVC 参照:1.3.2\shiro-root-1.3.2-source-release\shiro-ro ...