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:多个进程共享同一打印终 ...
随机推荐
- 【保姆级安装使用教程#1】Xshell与Xftp的下载、安装和使用
1. 下载 官网下载地址:==Xshell与Xftp下载地址== 当然也可以用鄙人的百度网盘连接下载这是链接:链接百度网盘下载Xshell与Xftp分别下载Xshell与Xftp 2. 安装Xshel ...
- DevOps平台建设的关键点是什么?
关键还是在人 找到一个「吃过猪肉,见过猪跑的」,你问他什么是猪,他自然比「没吃过猪肉,没见过猪跑的人」更了解猪.海豚海豚,你知道猪是什么样么?它都没上过陆地,这辈子都没见过猪,它哪知道猪是什么样. 有 ...
- 其它——CGI,FastCGI,WSGI,uWSGI,uwsgi一文搞懂
文章目录 CGI, FastCGI, WSGI, uWSGI, uwsgi一文搞懂 一 CGI 二 FastCGI 三 WSGI 四 uWSGI 五 uwsgi CGI, FastCGI, WSGI, ...
- 程序员必备:使用AI工具通义千问,没有广告没有假链接,比搜索引擎要便捷多了,回答还算满意
我提的问题是:使用Python连接Oracle,cx_Oracle和oracledb那个效率更高,功能更全,请列出他们的区别和实例代码 回复是: 以下是使用Python连接Oracle时,cx_Ora ...
- linux特殊权限rws和rwt
Linux文件,除了rwx这些权限外,还有一些特殊的权限,如rws.rwt. 1.s权限(setuid) 1.1 设置方法:chmod u+s 该位可以让普通用户以root用户的角色运行只有root帐 ...
- 基于Java Swing和BouncyCastle的证书生成工具
"Almost no one will remember what he had just not interested." - Nobody "几乎没有人会记得他所丝毫 ...
- mysql5.5自定义函数-计算并赋值
mysql自定义函数的写法 特别注意,将全部的定义现在最前面,然后再写其他语句,因为之前将定义变量和赋值混着写,死活报错.最后一个前同事高手"邢哥"解决. DELIMITER $$ ...
- Kubernetes:kube-apiserver 之 scheme(二)
接 Kubernetes:kube-apiserver 之 scheme(一). 2.2 资源 convert 上篇说到资源版本之间通过内部版本 __internal 进行资源转换.这里进一步扩展介绍 ...
- Hello-FPGA CoaXPress 2.0 FPGA HOST IP Core PCIe Demo User Manual
目录 1 说明 4 2 设备连接 7 3 VIVADO FPGA工程 8 4 调试说明 9 图 1‑1 资料目录 4 图 1‑2 VIVADO工程目录结构 5 图 1‑3 VS软件工程目录 5 图 1 ...
- C# 反序列化JSON
try { u = await WebAPIOperator.PostAsync("/v2.0/auth/user/user/login", new LoginRequestMod ...