对应 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. 《HelloGitHub》第 90 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  2. Springboot+Guava实现单机令牌桶限流

    令牌桶算法 系统会维护一个令牌(token)桶,以一个恒定的速度往桶里放入令牌(token),这时如果有请求进来想要被处理,则需要先从桶里获取一个令牌(token),当桶里没有令牌(token)可取时 ...

  3. PostgreSQL学习笔记-1.基础知识:创建、删除数据库和表格

    PostgreSQL 创建数据库 PostgreSQL 创建数据库可以用以下三种方式:1.使用 CREATE DATABASE SQL 语句来创建.2.使用 createdb 命令来创建.3.使用 p ...

  4. MySQL5.7版本单节点大数据量迁移到PXC8.0版本集群全记录-2

    本文主要记录57版本升级80版本的过程,供参考. ■ 57版本升级80版本注意事项 默认字符集由latin1变为utf8mb4 MyISAM系统表全部换成InnoDB表 sql_mode参数默认值变化 ...

  5. 如何在虚拟机上安装linux操纵系统

    1.下载linux操作系统的镜像文件(iso文件),官网链接(CentOS Mirrors List) (3)下载大小为4G 或者4.几G的iso镜像文件 2.下载我发的VMware Workstat ...

  6. 【Vue3响应式入门#01】Reactivity

    专栏分享:vue2源码专栏,vue3源码专栏,vue router源码专栏,玩具项目专栏,硬核推荐 欢迎各位ITer关注点赞收藏 背景 以下是柏成根据Vue3官方课程整理的响应式书面文档 - 第一节, ...

  7. 【译】A unit of profiling makes the allocations go away

    在 Visual Studio 17.8 Preview 2 中,我们更新了单元测试分析,允许你在性能分析器中使用任何可用的工具--而不仅仅是仪表工具.有了这个更改,可以很容易地快速分析孤立的小工作单 ...

  8. ORB-SLAM3测试

    (一)环境搭建教程 1.Ubuntu18.04从零开始搭建orb slam3及数据集测试:https://blog.csdn.net/Skether/article/details/131320852 ...

  9. JS异步任务的并行、串行,以及二者结合

    让多个异步任务按照我们的想法执行,是开发中常见的需求.今天我们就来捋一下,如何让多个异步任务并行,串行,以及并行串行相结合. 一.并行 并行是使用最多的方式,多个相互间没有依赖关系的异步任务,并行执行 ...

  10. 20.2 OpenSSL 非对称RSA加解密算法

    RSA算法是一种非对称加密算法,由三位数学家Rivest.Shamir和Adleman共同发明,以他们三人的名字首字母命名.RSA算法的安全性基于大数分解问题,即对于一个非常大的合数,将其分解为两个质 ...