【搜索】 Prime Path
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; int n, m;
const int N = 1e4 + 100;
int vis[N];
struct node
{
int x, step;
};
queue<node> Q; bool judge_prime(int x) //判断素数
{
if(x == 0 || x == 1)
return false;
else if(x == 2 || x == 3)
return true;
else
{
for(int i = 2; i <= (int)sqrt(x); i++)
if(x % i == 0)
return false;
return true;
}
} void BFS()
{
int X, STEP, i;
while(!Q.empty())
{
node tmp;
tmp = Q.front();
Q.pop();
X = tmp.x;
STEP = tmp.step;
if(X == m)
{
printf("%d\n",STEP);
return ;
}
for(i = 1; i <= 9; i += 2) //个位
{
int s = X / 10 * 10 + i;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 0; i <= 9; i++) //十位
{
int s = X / 100 * 100 + i * 10 + X % 10;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 0; i <= 9; i++) //百位
{
int s = X / 1000 * 1000 + i * 100 + X % 100;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
for(i = 1; i <= 9; i++) //千位
{
int s = i * 1000 + X % 1000;
if(s != X && !vis[s] && judge_prime(s))
{
vis[s] = 1;
node temp;
temp.x = s;
temp.step = STEP + 1;
Q.push(temp);
}
}
}
printf("Impossible\n");
return ;
} int main()
{
int t, i;
scanf("%d",&t);
while(t--)
{
while(!Q.empty()) Q.pop();
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
vis[n] = 1;
node tmp;
tmp.x = n;
tmp.step = 0;
Q.push(tmp);
BFS();
}
return 0;
}
【搜索】 Prime Path的更多相关文章
- kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126
题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- poj 3126 Prime Path(搜索专题)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20237 Accepted: 11282 Desc ...
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- JMeter学习(九)FTP测试计划(转载)
转载自 http://www.cnblogs.com/yangxia-test FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划 ...
- PHPActiveRecord 学习一
#连接数据库 <?phprequire_once dirname(__FILE__) . '/../../ActiveRecord.php'; // initialize ActiveRecor ...
- tabindex 去掉虚线
参考 https://bbs.csdn.net/topics/390165247 style="outline: none"
- inception+archery SQL审核平台
关闭防火墙和selinux 宿主机安装mysql,创建archery数据库,并给所有权限,允许远程连接到该数据库 grant all privileges on *.* to 'root'@'%' i ...
- zabbix3.0.4安装部署与SendEmail报警配置
MySQL:5.6.21 nginx:1.62 PHP:5.7 pcre:8.32 zabbix:3.0.4 LNMP安装步骤略过 # tar xvf zabbix-3.0.4.tar.gz # cd ...
- pta l2-7(家庭房产)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 题意:给定n个人的信息,包括其编号 ...
- TDD - 登录成功和失败
/** * Created by Administrator on 2017-04-06. */ @RunWith(SpringJUnit4ClassRunner.class)@SpringBootT ...
- http://www.rabbitmq.com/documentation.html
http://www.rabbitmq.com/documentation.html https://www.gitbook.com/book/geewu/rabbitmq-quick/details
- day 24 socket 黏包
socket 套接字的使用: tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端 server 端 import socket sk = socket.socket() # 实例化一个 ...
- 当前的开源SLAM方案
开源方案 传感器形式 地址链接 MonoSLAM 单目 https://github.com/hanmekim/SceneLib2 PTAM 单目 http://www.robots.ox.ac. ...