HDU 1976 prime path
题意:给你2个数n m。从n变成m最少须要改变多少次。
当中:
1、n m 都是4位数
2、每次仅仅能改变n的一个位数(个位、十位、百位、千位),且每次改变后后的新数为素数
思路:搜索的变形题,这次我们要搜得方向是改变位数中的一位,然后往下搜。直到求出我们须要的那个解
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define N 10000
int dis[N],cou[N];
bool prime[N];
void make() //素数打表
{
int i,j;
for(i=1000;i<=N;i++)
{
int flag=1;
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
prime[i]=false;
flag=0;
break;
}
}
if(flag)prime[i]=true;
} }
int bfs(int x,int y)
{
queue <int>q;
int v,i,j,temp,vtemp,t[4]; //t数组存放该数的每一位数
memset(dis,0,sizeof(dis));
memset(cou,0,sizeof(cou));
q.push(x);
dis[x]=1;
while(!q.empty())
{
v=q.front();
q.pop();
t[0]=v/1000;
t[1]=v%1000/100;
t[2]=v%100/10;
t[3]=v%10;
for(j=0;j<4;j++)
{
temp=t[j];
for(i=0;i<10;i++)
if(i!=temp)
{
t[j]=i;
vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];
if(!dis[vtemp]&&prime[vtemp]){
cou[vtemp]=cou[v]+1;
dis[vtemp]=1;
q.push(vtemp);
}
if(vtemp==y) return cou[vtemp];
}
t[j]=temp;
}
if(v==y) return cou[v];
}
return -1;
}
int main()
{
int t,n,m,sum;
make();
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
sum=bfs(n,m);
printf("%d\n",sum);
}
return 0;
}
HDU 1976 prime path的更多相关文章
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- 【BFS】hdu 1973 Prime Path
题目描述: http://poj.org/problem?id=3414 中文大意: 使用两个锅,盛取定量水. 两个锅的容量和目标水量由用户输入. 允许的操作有:灌满锅.倒光锅内的水.一个锅中的水倒入 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
随机推荐
- pickle模块
在编程中,如果存在大的列表或者字典,可以在python中引入pickle 模块: 例如:将下边这组列表保存到文件当中:[1, 2, 'xiaomao', '小狗'] 程序: import pickle ...
- linux下/proc/sysrq-trigger文件的功能
/proc/sysrq-trigger该文件能做些什么事情呢? # 立即重新启动计算机 (Reboots the kernel without first unmounting file system ...
- VHDL testbench 例子,包含向文件中写数据
LIBRARY ieee; USE ieee.std_logic_1164.ALL; use std.textio.all; use ieee.std_logic_textio.all; EN ...
- c/c++实现混合编程
在开发中大家经常会使用到c与c++混合编程,这样能够更好的实现功能模块.刚学习了一下c和c++的混合编程,参考了网上的相关知识,在这里留下要点,方便以后进行查阅. 1.extern关键字 extern ...
- <meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cac ...
- LINUX常用命令--重定向、管道篇(四)
一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...
- linux基础--chkconfig 详解
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...
- 在Windows平台下安装与配置Memcached的方法分享
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...
- 26个jQuery使用小技巧(25)
下面列出了一些Jquery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字.在新窗口中打开链接.检测浏览器.预加载图片.页面样式切换.所有列等高.动态控制页面字体大小.获得鼠标指针的X值Y值.验证元 ...
- Openstack 的 RPC使用。
大家都已经很熟悉了RPC了. 比如说nfs就是采用的RPC通信. 尤其SUN RPC 已经成为了C语言开发的经典一种进程间调用机制. openstack 的RPC 机制, 是基于AMQP 活着其他高级 ...