hdu 1195 Open the Lock (BFS)
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3388 Accepted Submission(s): 1499
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
1234
2144
1111
9999
4
//15MS 340K 1607 B C++
/* 题意:
给出两个四位数,有两种操作,相邻交换或每位+/-1;
求最少步数使第一串变为第二串 bfs:
细心点就可以过,先考虑每一次最多可以有多少步可以走,因为只有四位数,
而且有两种操作,一共有七步,相邻交换有三步,每位操作有四步,知道这个然后就常规的
bfs求解。 */
#include<iostream>
#include<queue>
using namespace std;
struct node{
int a,cnt;
};
int a,b;
int vis[];
int bfs()
{
memset(vis,,sizeof(vis));
queue<node>Q;
node t={a,};
vis[a]=;
Q.push(t);
while(!Q.empty()){
t=Q.front();
Q.pop();
if(t.a==b) return t.cnt;
node tt=t;
tt.cnt++;
tt.a=t.a%+(t.a/)*+(t.a%)/*;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("1*%d\n",tt.a);
}
tt.a=t.a/*+(t.a%)/*+(t.a%)/*+t.a%;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("2*%d\n",tt.a);
}
tt.a=t.a/*+(t.a%)*+(t.a%)/;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("3*%d\n",tt.a);
} for(int i=;i<;i*=){
int temp=(t.a%(i*))/i;
if(temp==) tt.a=t.a-*i;
else tt.a=t.a+i;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("4*%d\n",tt.a);
}
if(temp==) tt.a=t.a+*i;
else tt.a=t.a-i;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("5*%d\n",tt.a);
}
}
if(t.a<) return ;
}
return ;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
printf("%d\n",bfs());
}
return ;
}
hdu 1195 Open the Lock (BFS)的更多相关文章
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
随机推荐
- 3、SpringBoot+Mybatis整合------主键回填
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...
- 【杂题总汇】HDU-6406 Taotao Picks Apples
[HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了……找了正解对拍,在比赛结束后17分钟AC了
- Linux文件服务器实战(系统用户)
ftp匿名用户设置完成之后任何人都可以访问服务器端文件,目录,甚至可以修改删除文件和目录,,那如何存放私密文件并保证文件或者目录专属于拥有者呢,就需要使用vsftp系统用户来实现了. 1.在linux ...
- 在haoodp-2.7.3 HA的基础上安装Hbase HA
前提安装好hadoop基于QJM的高可用 node1 HMaster node2 HMaster.HRegionServer node3 HRegionServer node4 HRegionServ ...
- scrapy--json(360美图)
之前开始学习scrapy,接触了AJax异步加载.一直没放到自己博客,趁现在不忙,也准备为下一个爬虫做知识储存,就分享给大家. 还是从爬取图片开始,先上图给大家看看成果,QAQ. 一.图片加载的方法 ...
- linux 安装nginx yum
本分类下有一个环境一键安装.那这背后发生了什么呢?咱们手动使用源码进行安装.1.首先保证有一个能联网的centos.2.百度 ningx 官网 点download http://nginx.or ...
- CentOS7下安装FTP
1.安装vsftpd yum install -y vsftpd 2.设置 使用命令systemctl status vsftpd.service查看ftp状态. 开启ftp systemctl st ...
- 第一章 UNIX 基础知识
1.1 Unix体系结构 OS定义为一种软件,它控制计算机硬件资源,提供程序运行环境,一般称其为内核(kernel),它体积小,位于环境中心. 内核的接口为系统调用(system call),共用函数 ...
- keil调试问题记录
1.错误类型:L6218E:Underfined symbol &&&&&&&&& (referred form &&a ...
- git 设置别名 git alias
git config --global alias.st status git config --global alias.ck checkout git config --global alias. ...