对应 LeetCode 752.转动转盘锁

### 问题定义

你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有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(二)转动转盘锁的更多相关文章

  1. Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

    Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  2. [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', ' ...

  3. leetcode 752. 打开转盘锁

    地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...

  4. LeetCode 752:打开转盘锁 Open the Lock

    题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...

  5. [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', ...

  6. JVM内部细节之二:偏向锁(Biased Locking)

    在前面一片文章<JVM内部细节之一:synchronized关键字及实现细节>中已经提到过偏向锁的概念,在理解什么是偏向锁前必须先理解什么是轻量级锁(Lightweight Locking ...

  7. Java实现 LeetCode 752 打开转盘锁(暴力)

    752. 打开转盘锁 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋 ...

  8. 【Java并发编程实战】----- AQS(二):获取锁、释放锁

    上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...

  9. (删)Java线程同步实现二:Lock锁和Condition

    在上篇文章(3.Java多线程总结系列:Java的线程同步实现)中,我们介绍了用synchronized关键字实现线程同步.但在Java中还有一种方式可以实现线程同步,那就是Lock锁. 一.同步锁 ...

  10. python并发编程之多进程(二):互斥锁(同步锁)&进程其他属性&进程间通信(queue)&生产者消费者模型

    一,互斥锁,同步锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 竞争带来的结果就是错乱,如何控制,就是加锁处理 part1:多个进程共享同一打印终 ...

随机推荐

  1. Ubuntu更新软件的命令

    更新软件源 apt-get update 更新升级所有软件 apt-get upgrade 更新某个软件 apt-get upgrade 名 列出可更新的软件 apt list --upgradabl ...

  2. About Info-ZIP

    LATEST RELEASES: Zip 3.00 was released on 7 July 2008. WiZ 5.03 was released on 11 March 2005. UnZip ...

  3. mol 文件格式简单解析(v2000)

    前言 .mol 文件是常见的化学文件格式,主要包含分子的坐标.分子间的键等数据. 示例文件 下面是一个水分子的 .mol 文件 H2O APtclcactv05052315543D 0 0.00000 ...

  4. CF1526C1

    题目简化和分析: 给您一个数组,在其中选择若干个数使得: 任意前缀和 \(\ge 0\) 数量尽可能的大 我们可以使用贪心策略,策略如下: 如果当前数为非负,必喝. 而毒药尽可能的多喝,如果喝没了,就 ...

  5. CDQ分治和三维偏序

    专题:CDQ 分治 本页面将完整介绍 CDQ 分治. 简介 CDQ 分治是一种思想而不是具体的算法,与动态规划类似.目前这个思想的拓展十分广泛,依原理与写法的不同,大致分为三类: 解决和点对有关的问题 ...

  6. AI图形算法的应用之一:仪表识别

    目前AI智能算法如火如荼,各大型厂商对此趋之若鹜般加大开发力度,比如大华.海康这些视频处理类,以及百度.腾讯这些IT软件厂商,因为业务开展需要,我也把研发方向转向于此,小有成绩,在此展示一下. 最近研 ...

  7. 银河麒麟V10 修改文件夹权限

    并不建议修改系统文件夹的权限,防止终端失效 指令:获取所有权限 指令:写入可执行权限 chmod +x filename//filename 是文件路径 TRANSLATE with x Englis ...

  8. [Python急救站课程]整数数列求和

    整数数列求和程序: n = input("请输入整数N: ") sum = 0 for i in range(int(n)): # range用于计数整数.for表示循环 sum ...

  9. 题解 SP13015

    题目描述: 给定初始序列 \(A\),然后对原序列有以下操作: 操作 \(1\): 0 l r v 将区间 \([l,r]\) 全赋值为 \(v\). 操作 \(2\):1 l r 查询区间 \([l ...

  10. 火山引擎 DataLeap 计算治理自动化解决方案实践和思考

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 [导读]本文旨在探讨火山引擎 DataLeap 在处理计算治理过程中所面临的问题及其解决方案,并展示这些解决方案带 ...