BFS(二)转动转盘锁
### 问题定义
你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 。每个拨轮可以自由旋转:例如把 '9' 变为 '0','0' 变为 '9' 。每次旋转都只能旋转一个拨轮的一位数字。
锁的初始数字为 '0000' ,一个代表四个拨轮的数字的字符串
列表 deads
包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。
字符串 target 代表可以解锁的数字,你需要给出解锁需要的最小旋转次数,如果无论如何不能解锁,返回 -1 。
解决思路
穷举
这个问题比较简单,使用穷举的方式列出从 "0000" 开始满足所有条件的转动情况,进行转动分析即可
这里的穷举使用
BFS
是一个很好的思路,每层的高度就对应着转动的次数,只要当前层中存在目标数字 \(target\) ,那么当前的层数就是其搜索的次数双向
BFS
优化由于每层的每个数字的都可以向上或向下转动,因此在搜索过程中将会出现 “搜索爆炸” 的情况。可选的解决方案交替使用从上和从下的搜索方式进行遍历,这样就能够有效地解决 “搜索爆炸” 的问题
细节处理:实际上,有些被搜索过的数字可能在之后会再次出现,因此需要记录之前已经搜索过的数字;使用双向搜索的方式进行搜索时,使用 Map
来记录两个方向的搜索次数,当搜索成功时相加即可。
实现
一般的穷举
class Solution {
Set<String> deadSet = new HashSet<>();
static String start = "0000";
Set<String> accessed = new HashSet<>(); public int openLock(String[] deadends, String target) {
for (String s : deadends) deadSet.add(s); // 特殊情况处理
if (deadSet.contains(start) || deadSet.contains(target))
return -1;
if (target.equals(start)) return 0; Deque<String> deque = new LinkedList<>();
deque.offer(start);
accessed.add(start); int ans = 0;
while (!deque.isEmpty()) {
int size = deque.size();
ans++; while (size-- > 0) {
String word = deque.poll();
for (int i = 0; i < 4; ++i) {
String plus = plus(word.toCharArray(), i);
if (!deadSet.contains(plus) && !accessed.contains(plus)) {
if (plus.equals(target)) return ans;
deque.offer(plus);
accessed.add(plus);
} String minus = minus(word.toCharArray(), i);
if (!deadSet.contains(minus) && !accessed.contains(minus)) {
if (minus.equals(target)) return ans;
deque.offer(minus);
accessed.add(minus);
}
}
}
} return -1;
} // 指定的数字位 +1
String plus(char[] array, int index) {
if (array[index] < '9') array[index] = (char) (array[index] + 1);
else array[index] = '0'; return String.valueOf(array);
} // 指定的数字位 -1
String minus(char[] array, int index) {
if (array[index] > '0') array[index] = (char) (array[index] - 1);
else array[index] = '9'; return String.valueOf(array);
}
}
复杂度分析:略
双向
BFS
class Solution {
Set<String> deadSet = new HashSet<>();
static String start = "0000";
Set<String> accessed = new HashSet<>(); public int openLock(String[] deadends, String target) {
for (String s : deadends) deadSet.add(s); if (deadSet.contains(start) || deadSet.contains(target))
return -1;
if (target.equals(start)) return 0; Deque<String> top = new LinkedList<>();
Deque<String> bottom = new LinkedList<>();
Map<String, Integer> topMap = new HashMap<>();
Map<String, Integer> bottomMap = new HashMap<>(); top.offer(start);
topMap.put(start, 0); bottom.offer(target);
bottomMap.put(target, 0); while (!top.isEmpty() && !bottom.isEmpty()) {
int t = -1;
if (top.size() <= bottom.size()) {
t = update(top, bottom, topMap, bottomMap);
} else {
t = update(bottom, top, bottomMap, topMap);
} if (t != -1) return t;
} return -1;
} int update(
Deque<String> d1,
Deque<String> d2,
Map<String, Integer> map1,
Map<String, Integer> map2
) {
int size = d1.size(); while (size-- > 0) {
String word = d1.poll(); for (int i = 0; i < 4; ++i) {
String plus = plus(word.toCharArray(), i);
if (!deadSet.contains(plus) && !map1.containsKey(plus)) {
if (map2.containsKey(plus))
return map1.get(word) + map2.get(plus) + 1; // 本次已经再转动了一次
d1.offer(plus);
map1.put(plus, map1.get(word) + 1);
} String minus = minus(word.toCharArray(), i);
if (!deadSet.contains(minus) && !map1.containsKey(minus)) {
if (map2.containsKey(minus))
return map1.get(word) + map2.get(minus) + 1;
d1.offer(minus);
map1.put(minus, map1.get(word) + 1);
}
}
} return -1;
} String plus(char[] array, int index) {
if (array[index] < '9') array[index] = (char) (array[index] + 1);
else array[index] = '0'; return String.valueOf(array);
} String minus(char[] array, int index) {
if (array[index] > '0') array[index] = (char) (array[index] - 1);
else array[index] = '9'; return String.valueOf(array);
}
}
复杂度分析:略
BFS(二)转动转盘锁的更多相关文章
- Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)
Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- [LeetCode] 0752. Open the Lock 打开转盘锁
题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...
- leetcode 752. 打开转盘锁
地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...
- LeetCode 752:打开转盘锁 Open the Lock
题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...
- [Swift]LeetCode752. 打开转盘锁 | Open the Lock
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- JVM内部细节之二:偏向锁(Biased Locking)
在前面一片文章<JVM内部细节之一:synchronized关键字及实现细节>中已经提到过偏向锁的概念,在理解什么是偏向锁前必须先理解什么是轻量级锁(Lightweight Locking ...
- Java实现 LeetCode 752 打开转盘锁(暴力)
752. 打开转盘锁 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋 ...
- 【Java并发编程实战】----- AQS(二):获取锁、释放锁
上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...
- (删)Java线程同步实现二:Lock锁和Condition
在上篇文章(3.Java多线程总结系列:Java的线程同步实现)中,我们介绍了用synchronized关键字实现线程同步.但在Java中还有一种方式可以实现线程同步,那就是Lock锁. 一.同步锁 ...
- python并发编程之多进程(二):互斥锁(同步锁)&进程其他属性&进程间通信(queue)&生产者消费者模型
一,互斥锁,同步锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 竞争带来的结果就是错乱,如何控制,就是加锁处理 part1:多个进程共享同一打印终 ...
随机推荐
- 《流畅的Python》 读书笔记 230926(第一章后半部分)
1.2 如何使用特殊方法 特殊方法的存在是为了被 Python 解释器调用的,你自己并不需要调用它们 就是说通常你都应该用len(obj)而不是obj.__len()__,无论是系统预置的,还是你自己 ...
- 整理DB2左补零,右补零的方法
在项目中经常遇到需要左补零,右补零的情况,在DB2实验环境中展示 1.左补零(1)数字左补零,数字长度不定用right(digits(cast(expression as bigint)),NUM)能 ...
- 【知识杂谈#1】Linux如何安装net-tools和sbin配置PATH
1. Linux下载net-tools 在Linux上下载net-tools包的方法可能会因你所使用的Linux发行版而有所不同.在某些现代的Linux发行版中,net-tools已经被弃用,而推荐使 ...
- 第一个 Go 程序"hello,world" 与 main 函数和Go常用基本命令
第一个 Go 程序"hello,world" 与 main 函数和Go常用基本命令 目录 第一个 Go 程序"hello,world" 与 main 函数和Go ...
- 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(8) -- 使用Converter类实现内容的转义处理
在我们WPF应用端的时候,和WInform开发或者Vue前端开发一样,有时候也需要对内容进行转义处理,如把一些0,1数值转换为具体含义的文本信息,或者把一些布尔变量转换为是否等,都是常见的转换处理,本 ...
- 漫谈C#的定时执行程序
1.写法1 task的lambda表达式 #region 写法1 task的lambda表达式 //static void Main() //{ // // 创建并启动两个任务 // Task tas ...
- Spring/SpringBoot中的声明式事务和编程式事务源码、区别、优缺点、适用场景、实战
一.前言 在现代软件开发中,事务处理是必不可少的一部分.当多个操作需要作为一个整体来执行时,事务可以确保数据的完整性和一致性,并避免出现异常和错误情况.在SpringBoot框架中,我们可以使用声明式 ...
- JavaScript高级程序设计笔记09 代理与反射
代理与反射 ES6新增的代理和反射为开发者提供了拦截并向基本操作嵌入额外行为的能力. 具体就是,可以给目标对象定义一个关联的代理对象,而这个代理对象可以作为抽象的目标对象来使用. 在对目标对象的各种操 ...
- JSX、TSX 整体理解
可以少去理解一些不必要的概念,而多去思考为什么会有这样的东西,它解决了什么问题,或者它的运行机制是什么? JS JavaScript 是互联网上最流行的脚本语言,这门语言可用于 HTML 和 web, ...
- JAVAweek5
学习内容 进制 1.(十进制):752=2*10(0)+5*10(1)0+7*10(2)=752 (二进制):1011(二进制的数)=1*2(0)+1*2(1)+0*2(2)+1*2(3) = 1 ...