POJ3126——Prime Path
非常水的一道广搜题(专业刷水题)。
。。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define inf 1000000
using namespace std;
int d[4]={1,10,100,1000};
int vis[10000];
int prime[10000];
int q[inf];
int n1,n2;
void isprime()
{
memset(prime,1,sizeof prime);
for(int i=2;i<10000;i++){
if(prime[i]){
for(int j=i*i;j<10000;j+=i){
prime[j]=0;
}
}
}
} void bfs()
{
int front=0,rear=0;
memset(q,0,sizeof(q));
memset(vis,0,sizeof( vis));
vis[n1]=1;
q[rear++]=n1;
while(front<rear){
int cur=q[front++];
for(int i=0;i<4;i++){
int t[4];
t[3]=cur/1000,t[2]=cur/100%10,t[1]=cur/10%10,t[0]=cur%10; for(int j=0;j<=9;j++){
if(j==t[i]) continue;
if(i==3&&j==0) continue;
else{
int next=(cur-t[i]*d[i])+j*d[i];
if(prime[next]&&!vis[next]){
if(next==n2){
cout<<vis[cur]<<endl;return;
}
else{
vis[next]=vis[cur]+1;
q[rear++]=next;
} }
}
}
} }
cout<<"0"<<endl;
} int main()
{
isprime();
int T;
cin>>T;
while(T--){
cin>>n1>>n2;
bfs();
}
return 0;
}
POJ3126——Prime Path的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path
http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
随机推荐
- linux更换阿里云的源的shell脚本
#!/bin/bash##########################################Function: update source#Usage: bash update_sour ...
- Hadoop一主一从部署(2)
Hadoop部署一主一从(2) 1.关闭防火墙和Linux守护进程 执行命令: iptables -F setenforce 0 2.对Hadoop集群进行初始化,在namenode(主机)上执行命令 ...
- [转]利用 NPOI 變更字體尺寸及樣式
本文转自:http://blog.cscworm.net/?p=1650 利用 NPOI 變更字體尺寸及樣式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 3d数学 7 矩阵
7.1 矩阵-数学定义 在线性代数中, 矩阵就是以行和列形式组织的矩形数字块.矩阵是向量的数组. 7.1.1 矩阵的维度和记法 矩阵的维度被定义为它包含了多少行和多少列.一个\(r \times c\ ...
- css的选择器效率分析
我们都知道,CSS具有叠加性(同一个元素被多条样式规则指定),继承性(后代元素会继承前辈元素的一些样式和属性)和优先级 (由于CSS的叠加性和继承性,将产生优先级,这指的是哪条样式规则会最终作用于指定 ...
- Css小动画
html页面: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF ...
- 查看Oracle数据库表空间大小,是否需要增加表空间的数据文件
在数据库管理中,磁盘空间不足是DBA都会遇到的问题,问题比较常见. --1查看表空间已经使用的百分比 Sql代码 select a.tablespace_name,a.bytes/1024/1024 ...
- 从XMLHttpRequest中获取请求的URL
在编写Ajax通用错误处理程序时,经常需要记录发生错误的XMLHttpRequest的请求URL.但查询文档,并未找到从XMLHttpRequest中获取请求URL的方法. 在javascript - ...
- html 复杂表格
123456789 123456789 0000000000 日期 123456789 1234560000000789 ----------- ----------- ----------- --- ...
- vue货币格式化组件、局部过滤功能以及全局过滤功能
一.在这里介绍一个vue的时间格式化插件: moment 使用方法: .npm install moment --save. 2 定义时间格式化全局过滤器 在main.js中 导入组件 import ...