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 ...
随机推荐
- JQuery 过滤选择器 与属性修改的方法演示比较
文本匹配 在表单输入项里面输入值,根据输入值,点击判断按钮,让对应的复选框选中 <html> <head> <meta http-equiv="Content- ...
- Pop–实现任意iOS对象的任意属性的动态变化
简介 Pop 是一个可扩展的动画引擎,可用于实现任意iOS对象的任意属性的动态变化,支持一般动画,弹性动画和渐变动画三种类型. 项目主页: pop 最新示例: 点击下载 注意: 官方代码中,并不包含实 ...
- 深入浅出:了解JavaScript中的call,apply,bind的差别
在 javascript之 this 关键字详解文章中,谈及了如下内容,做一个简单的回顾: 1.this对象的涵义就是指向当前对象中的属性和方法. 2.this指向的可变 ...
- github上更新fork项目
转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...
- 1060: [ZJOI2007]时态同步
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3610 Solved: 1521[Submit][Status][Discuss] Descript ...
- LNMP+HAProxy+Keepalived负载均衡 - 基础服务准备
日志服务 修改日志服务配置并重启日志服务: ``` vim /etc/rsyslog.conf ``` 编辑系统日志配置,指定包含的配置文件路径和规则: ``` $IncludeConfig /etc ...
- jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法
我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...
- python分布式爬虫--房天下
第一步安装redis redis在windows系统中的安装与启动: 下载:redis官方是不支持windows操作系统的.但是微软的开源部门将redis移植到了windows上.因此下载地址不是在r ...
- C++基础 C++对类的管理——封装
1.封装 两层含义: (1)把事物的属性和方法结合成个整体. (2)对类的属性和方法进行访问控制,对不信的进行信息屏蔽. 2.访问控制 控制分为 类的内部,类的外部. public 修饰的成员,可在内 ...
- poj 1957 二分搜索
题意:N个灯泡离地H_i,满足H1 = A ,Hi = (Hi-1 + Hi+1)/2 – 1,HN = B ,求最小B. 思路: 只要二分第二个灯泡的高度就可以推出全部灯泡的高度 如果hi<0 ...