poj 3126
一道搜索的水题,其实搜索相对其他的来说好掌握一点,因为有个固定的模板可以用去套
题目大意就是数字的变化,一个数字只可以变化到它最相邻的一个素数上去,意思是只变化一位数字,求最短的变化方案
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue> bool mark [10001]; int s[10001],step[10001],ans; using namespace std; queue<int >q; int bfs(int x)
{
int head,c[4],y,i;
if(x==ans)
{
printf("0\n");
return 0;
}
while(!q.empty())
q.pop();
q.push(x);
mark[x]=false;
while(!q.empty())
{
head=q.front();
q.pop();
c[0]=head/1000;
c[1]=(head-c[0]*1000)/100;
c[2]=(head-c[0]*1000-c[1]*100)/10;
c[3]=head%10;
for(i=0;i<=9;i++)
{
if(i!=0)
{
y=1000*i+c[1]*100+c[2]*10+c[3];
if(s[y]&&mark[y]){q.push(y);mark[y]=false;step[y]=step[head]+1;}
}
y=c[0]*1000+100*i+c[2]*10+c[3];
if(s[y]&&mark[y]){q.push(y);mark[y]=false;step[y]=step[head]+1;}
y=c[0]*1000+c[1]*100+i*10+c[3];
if(s[y]&&mark[y]){q.push(y);mark[y]=false;step[y]=step[head]+1;}
y=c[0]*1000+c[1]*100+c[2]*10+i;
if(s[y]&&mark[y]){q.push(y);mark[y]=false;step[y]=step[head]+1;}
}
if(head==ans) printf("%d\n",step[head]);
}
return 0;
}
int main()
{
int i,n,first;
memset(s,0,sizeof(s));
for(i=1000;i<=10000;i++)
for(int j=2;j<=sqrt(i)+1;j++)
{
if(i%j==0) break;
if(j==int(sqrt(i))) s[i]=1;
}
scanf("%d",&n);
while(n)
{
n--;
memset(mark,true,sizeof(mark));
memset(step,0,sizeof(step));
scanf("%d%d",&first,&ans);
bfs(first);
}
return 0;
}
poj 3126的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
随机推荐
- php正则表达式匹配用户名规则:由字母开头的6-16位字母和数字组成的字符串
$pattern = '/^[a-zA-Z]{1}[a-zA-Z0-9]{5,15}$/i';
- C语言中的EOF和回车不一样
经常我们碰到这样一个C语言问题,例如: 输入一个组整数,按照从小到大排序后输出结果 输入: 1 7 9 2 4 输出: 1 2 4 7 9 这里要用C语言读入一段数的话,如果用 int array ...
- Django URLconf
Django提供了干净优雅的 URL 方案,URL配置文件是一个标准的 python 文件,支持动态配置.它的本质就是URL模式与调用的视图函数之间的映射表,最简单的配置文件如下: from djan ...
- 大熊君大话NodeJS之------FS文件模块
一,开篇分析 文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集.可以通过调用 require("fs") 来获取该模块.文件系统模块中的所有方法均有异步和同步 ...
- 如何在发布博客时插入复杂公式——Open Live Writer
1.http://latex.codecogs.com/eqneditor/editor.php 2.使用Word发布
- web性能调优
http://blog.csdn.net/chengzhezhijian/article/details/50680250 Java Web应用调优线程池:没你想的那么复杂 标签: java 线程池 ...
- C#3.0 特性
C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...
- POJ 2635 The Embarrassed Cryptographer
大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...
- 64.SHELL
SHELL 1. crontab定时器 编辑使用crontab -e 一共6列,分别是:分 时 日 月 周 命令 查看使用crontab -l 删除任务crontab -r 查看crontab执行日志 ...
- 推荐系统(协同过滤,slope one)
1.推荐系统中的算法: 协同过滤: 基于用户 user-cf 基于内容 item –cf slop one 关联规则 (Apriori 算法,啤酒与尿布) 2.slope one 算法 slope o ...