题意为给出两个四位素数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)的更多相关文章

  1. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  3. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  4. poj3216 Prime Path(BFS)

    题目传送门  Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Sec ...

  5. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  8. POJ - 3126 - Prime Path(BFS)

    Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...

  9. POJ-3126-Prime Path(BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27852   Accepted: 15204 Desc ...

随机推荐

  1. CQRS框架:AxonFramework 之 Hello World

    Command Query Responsibility Segregation,CQRS 这个架构好象最近博客园里讨论得比较多,有几篇园友的文章很有深度,推荐阅读: CQRS架构简介 浅谈命令查询职 ...

  2. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  3. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  4. 如何在ASP.NET Core中使用Redis

    注:本文提到的代码示例下载地址> https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418 Redis是一个开源的内 ...

  5. kubernetes 1.4.5集群部署

    2016/11/16 23:39:58 环境: centos7 [fu@centos server]$ uname -a Linux centos 3.10.0-327.el7.x86_64 #1 S ...

  6. weui 图片弹框

    添加图片与弹出效果对比: HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. android-配置文件AndroidManifest.xml

    AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实 ...

  8. 【转】OpenGL超级宝典笔记——纹理映射Mipmap

    原文地址 http://my.oschina.net/sweetdark/blog/177812 , 感谢作者,若非法转载请联系本人. 目录[-] Mipmapping Mipmap过滤 构建Mip层 ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  10. MySQL、MongoDB、Redis数据库Docker镜像制作

    MySQL.MongoDB.Redis数据库Docker镜像制作 在多台主机上进行数据库部署时,如果使用传统的MySQL的交互式的安装方式将会重复很多遍.如果做成镜像,那么我们只需要make once ...