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 ...
随机推荐
- PHP_OOP
1.存储器方法——用于限制对象的变量属性 对于弱类型的PHP,存储器方法来限制变量属性显得非常重要! 通过为所有属性创建存储器方法,可以简化添加数据验证或新的业务逻辑的工作,也可以简化在后边对对象执行 ...
- 搞了一个独立博客,请各位光临pingworld.cn
嘿嘿,每次在一个大网站上建立自己的博客后总是没有动力持续更新下去,回想其原因很大是因为没有一个自己的地盘,懒得维护!还有一个原因就是自己也没有什么干货值得跟大家分享. 随着工作的时日见长,有了各种各样 ...
- 关于C语言中结构体中的结构体成员导致的字节对齐问题
关于结构体的字节对齐是什么,就不赘述,再此附上一篇文章,介绍字节对齐:http://www.linuxsong.org/2010/09/c-byte-alignment/ 这里的结构体字节对齐的数据类 ...
- 製程能力介紹(SPC introduction) ─ Cp之製程能力解釋
Cp之製程能力解釋 從常態分配的特性來看,在群體中 ±3σ(標準差) 之範圍內的值,應包含群體全部的 99.73%.也就是說,若以 6σ為單位,就可以代表整個分布的範圍,但是有 0.27% (2700 ...
- 保存网页为图片——滚动截取IE(WebBrowse)
对IE进行编程一直觉得是相当可怕的事情,里面的接口.函数.事件之多,解释之乱,需要了解的方方面面知识之博,让我仿佛看到了微软就是造物主,因为它已成功制造了这样的混沌,弄就了宇宙的初始状态…… 近来做个 ...
- InputStream、OutputStream、String的相互转换(转)
//1.字符串转inputStream String string; //...... InputStream is = new ByteArrayInputStream(string.getByte ...
- Unix/Linux环境C编程入门教程(1) Solaris 11 64bit环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装Solaris 11 64位并且配置好C/C++开发环境 本文所 ...
- Error copying image in the datastore: Not allowed to copy image file
opennebula error copying image in the datastore not allowed to copy image file Error copying image i ...
- JDBC增强
JDBC增强 批处理:批量处理sql语句,比如批量添加用户信息. addBatch() //pstmt.addBatch() 就是替换一条一条执行的execute****** executeBat ...
- SlidingMenu的编译及使用
1. 在github上有一个效果不错的开源库,SlidingMenu 最新的代码下载下来后,ExampleListActivity项目会报错: No resource found that ...