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 ...
随机推荐
- 14招搞定JavaScript调试
14招搞定JavaScript调试 译者按: 很多时候,大家可能只是依靠console.log来调试JavaScript代码,这样做的局限性不言而喻,这篇博客将教你几招实用的调试技巧. 原文: The ...
- Django day07 (二)单表操作
单表操作 -mysql数据库:settings里配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME ...
- El表达式日期处理
第1步:引入指令 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt " %&g ...
- Oracle 12.2.0.1 RAC for rhel 7.X 数据库安装(节点1执行root.sh失败)
说明: 最开始是用的rehat7.2安装12.2.0.1,后面安装GI节点一执行root.sh脚本失败,排查原因,最开始以为是操作系统的问题,换成rehat7.6,同样的出现问题,经过一番折腾,后面通 ...
- MySQL安装for windows
======MySQL安装 for windows====== 版本5.7.X MySQL服务器帮助我们来管理文件的操作 MySQL软件 - 服务器端软件 - 服务端程序 - 解析指令 - 对文件的操 ...
- .net中实现aspnetpager分页
第一步首先导入aspnetpager控件,然后再把他从工具箱中拖出,代码如下: <webdiyer:AspNetPager ID="aspnetpager1" runat= ...
- Windows phone开发 网络编程之HttpWebRequest
HttpWebRequest和WebClient的区别1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();2,其Method指定了请求 ...
- (转)用JS实现表格中隔行显示不同颜色
用JS实现表格中隔行显示不同颜色 第一种: <style> tr{bgColor:expression( this.bgColor=((this.rowIndex)%2==0 )? ...
- DeltaFish 校园物资共享平台 第一次小组会议
软工小组第一次会议 会议地点:图书馆 会议时间:19:00 ~ 20:00 与会人员:软工小组全体成员 请假人员:无缺席人员:无 记录人:陈志锴 整理人:曾子轩 会议记录 一.确认选题 每一位成员提出 ...
- fusionchart简单demo柱状图
本篇是柱状图,想要折线图的话,只要改变.swf文件就行. <div id="column2" style="width:240px; height:200px; m ...