题意:就是给你n组的四位数,在一次变化中又一位数字可以变化,而变化的方式为加一减一或者是与隔壁的互换,注意,是每一个数字都可以,

求最少的变化次数到达目标的数字

一看这个就应该知道这是一个bfs的题目,广搜么,不过要注意的就是标记,不然很有可能也出不来

我的代码写的比较繁琐,也比较糙,这个写的太复杂了,不过你也可以用循环来代替我的大部分代码,可以减少很多的书写量

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue> using namespace std;
int ans; queue<int >s; bool mark[];
int step[]; int bfs(int x)
{
int he,y,c[];while(!s.empty()) s.pop();
if(x==ans) return ;
s.push(x);
mark[x]=false;
while(!s.empty())
{
he=s.front();
s.pop();
c[]=he/; //这个的目的就是判断每一位数字是否为9或者1,因为9加1就是1了,而1减1也会变成9,
c[]=(he-c[]*)/;
c[]=(he-c[]*-c[]*)/;
c[]=he%;
if(c[]==&&mark[he-]){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
else if(mark[he+]&&c[]!=){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}} //切记这里要加一个那位数不能为9或者1,否则可能出现3位数的情况
if(c[]==&&mark[he-]){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
else if(mark[he+]&&c[]!=){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
if(c[]==&&mark[he-]){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
else if(mark[he+]&&c[]!=){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
if(c[]==&&mark[he-]){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
else if(mark[he+]&&c[]!=){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
if(c[]==&&mark[he+]){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
else if(mark[he-]&&c[]!=){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
if(c[]==&&mark[he+]){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
else if(mark[he-]&&c[]!=){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
if(c[]==&&mark[he+]){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
else if(mark[he-]&&c[]!=){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
if(c[]==&&mark[he+]){s.push(he+);mark[he+]=false;step[he+]=step[he]+;if(he+==ans){printf("%d\n",step[he+]);return ;}}
else if(mark[he-]&&c[]!=){s.push(he-);mark[he-]=false;step[he-]=step[he]+;if(he-==ans){printf("%d\n",step[he-]);return ;}}
y=c[]*+c[]*+c[]*+c[]; //这个就是与隔壁的交换,只有三种情况,就是每一个都与右边的交换。
if(mark[y]){s.push(y);mark[y]=false;step[y]=step[he]+;if(y==ans){printf("%d\n",step[y]);return ;}}
y=c[]*+c[]*+c[]*+c[];
if(mark[y]){s.push(y);mark[y]=false;step[y]=step[he]+;if(y==ans){printf("%d\n",step[y]);return ;}}
y=c[]*+c[]*+c[]*+c[];
if(mark[y]){s.push(y);mark[y]=false;step[y]=step[he]+;if(y==ans){printf("%d\n",step[y]);return ;}}
if(he==ans)
{
printf("%d\n",step[he]);
return ;
}
}
return ;
} int main()
{
int n,start;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&start,&ans);
memset(mark,true,sizeof(mark));
memset(step,,sizeof(step));
step[start]=;
bfs(start);
}
return ;
}

hdu 1195的更多相关文章

  1. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  2. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  3. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

  5. hdu 1195 广度搜索

    这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的.需要注意的时,加个vi[]数组,判读当前数是否已经存在于队列中.我做的很烦啊~~~ #include&l ...

  6. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...

  7. hdu 1195 Open the Lock (BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. hdu 1195(搜索)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

随机推荐

  1. 新浪微博客户端(34)-block的细节与本质

    main.m #import <Foundation/Foundation.h> void test4(); int main(int argc, const char * argv[]) ...

  2. editplus如何配置php编译环境?

    为什么要配置php编译? 因为,要先看看 php文件是否能够 编译得过去, 有没有错误, 如果有错误, 不能通过编译, 则肯定不能运行. 所以, 可以先看一下编译 得不得行. 在preferences ...

  3. centos 安装 mysql5.7.9初始密码问题

    mysql5.7.9在安装完成后会,root用户会产生一个不为空的初始密码,登陆mysql就会产生问题了,有必要修改一下登陆密码: 这是从网上找的一个方法,加以总结得出来的,亲测可以:# /etc/i ...

  4. oracle唯一索引与普通索引的区别和联系以及using index用法

    oracle唯一索引与普通索引的区别和联系 区别:唯一索引unique index和一般索引normal index最大的差异是在索引列上增加一层唯一约束.添加唯一索引的数据列可以为空,但是只要尊在数 ...

  5. Google翻译请求(难点是tk参数)

    业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...

  6. MySQL数据库的事务管理

    当前在开发ERP系统,使用到的数据库为Mysql.下面介绍下如何开启事务,以及事务隔离的机制 : 1. 检查当前数据库使用的存储引擎. show engines; 2. 修改前my.ini中的文件如下 ...

  7. 大数据之pig安装

    大数据之pig安装 1.下载 pig download 2. 解压安装 mapreduce模式安装: 1:设置HADOOP_HOME,如果pig所在节点不是集群中的节点,那就需要把集群中使用的hado ...

  8. MySQL监控系统MySQL MTOP的搭建(转VIII)

    MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统.系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理.最重要是MySQL服务器无需安装任何Agent,只 ...

  9. window使用qt遇到的坑

    1.window上的qt对图片的检测与识别不够完善.往往改了一个ui的背景图片,运行出来显示的却是旧的背景图片. 原因:  由于之前是项目与项目之间整合在一起,然后把ui_*_ui.h的临时文件也放在 ...

  10. php配置中的register_globals用法

    开发的时候设置成register_globals=off,只能通过post或get得到前端数据. 参考资料:http://blog.csdn.net/alex_best/article/details ...