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);
}
}
复杂度分析:略
双向
BFSclass 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:多个进程共享同一打印终 ...
随机推荐
- Redis系列23:性能优化指南
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- 手动实现Transformer
Transformer和BERT可谓是LLM的基础模型,彻底搞懂极其必要.Transformer最初设想是作为文本翻译模型使用的,而BERT模型构建使用了Transformer的部分组件,如果理解 ...
- 快速添加string value Refactor->android->Extract Android String 或按Ctrl+1 出现列表框选择Extract Android String 来进行String国际化
快速添加string value Refactor->android->Extract Android String或按Ctrl+1 出现列表框选择Extract Android Stri ...
- Java-全网最详细数据结构
数构&算法:数据结构 数据结构是计算机存储.组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合.通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率.数据结构往 ...
- 如何在linux(Ubuntu)下安装unity(Unity engine游戏引擎)
如果直接从unity官网下载unityhub的deb包,直接安装有可能出现unityhub打不开/打开缓慢/无法登陆/无法申请密钥等问题. 正解:从Unity官方源下载unity 1.先添加unity ...
- hammer.js学习
demo:https://github.com/fei1314/HammerJs/tree/master 知识点: hammer--手势识别:点击.长按.滑动.拖动.旋转.缩放 方法: tap 快速的 ...
- QT中级(2)QTableView自定义委托(二)实现QProgressBar委托
同系列文章 QT中级(1)QTableView自定义委托(一)实现QSpinBox.QDoubleSpinBox委托 QT中级(2)QTableView自定义委托(二)实现QProgressBar委托 ...
- 整理unity资料
https://www.cnblogs.com/fly-100/p/3910515.html 协同的概念介绍
- Seaurl-分享一个云上网址收藏网站
前言 最近网上发现一个强大的网址收藏网站,点击这里打开,分享给大家,希望大家会喜欢. 网址空间 "网址空间"是一个专业的在线平台,它允许用户分享他们在日常生活和工作中频繁访问的网站 ...
- 查看API官方文档
盛年不重来,一日难再晨.及时宜自勉,岁月不待人. 学习一门语言, 了解其开发文档必不可少.开发文档就是我们在编程开发.维护.升级过程中的参考最全面.最权威的资料.我认为就是开发者为后人方便理解留下 ...