打开转盘锁 -- BFS

打开转盘锁

这里提供两种实现,单向BFS和双向BFS。

python 复制代码
class OpenLock:
    """
    752. 打开转盘锁
    https://leetcode.cn/problems/open-the-lock/
    """
    def solution(self, deadends: List[str], target: str) -> int:
        """
        单向BFS
        :param deadends: 
        :param target: 
        :return: 
        """
        visited = set()
        # 将deadends初始化到visited数组中
        for deadend in deadends:
            visited.add(deadend)

        queue = []
        step = 0
        queue.append('0000')
        visited.add('0000')

        while queue:
            sz = len(queue)
            for i in range(sz):
                cur = queue.pop(0)

                if cur == target:
                    return step

                if cur in visited:
                    continue

                visited.add(cur)

                for j in range(4):
                    up = self.plusOne(cur, j)
                    if up not in visited:
                        queue.append(up)

                    down = self.minusOne(cur, j)
                    if down not in visited:
                        queue.append(down)

            step += 1
        return -1

    def plusOne(self, cur, j):
        if cur[j] == '9':
            return cur[:j] + '0' + cur[j+1:]
        else:
            return cur[:j] + str(int(cur[j])+1) + cur[j+1:]

    def minusOne(self, cur, j):
        if cur[j] == '0':
            return cur[:j] + '9' + cur[j + 1:]
        else:
            return cur[:j] + str(int(cur[j]) - 1) + cur[j + 1:]

    def solution2(self, deadends: List[str], target: str) -> int:
        """
        双向BFS优化
        :param deadends: 
        :param target: 
        :return: 
        """
        deads = set()
        visited = set()
        # 将deadends初始化到visited数组中
        for deadend in deadends:
            deads.add(deadend)

        q1 = set()
        q2 = set()
        q1.add('0000')
        q2.add(target)
        step = 0

        while q1 and q2:
            # 额外优化
            if len(q1) > len(q2):
                tmp = q1
                q1 = q2
                q2 = tmp

            temp = set()
            for cur in q1:
                if cur in deads:
                    continue
                if cur in q2:
                    return step

                visited.add(cur)

                for j in range(4):
                    up = self.plusOne(cur, j)
                    if up not in visited:
                        temp.add(up)

                    down = self.minusOne(cur, j)
                    if down not in visited:
                        temp.add(down)

            step += 1
            # 这里temp相当于q1,交换q1和q2
            q1 = q2
            q2 = temp

        return -1
相关推荐
仍然.5 天前
算法题目---BFS解决最短路问题
算法·宽度优先
进击的荆棘6 天前
优选算法——BFS
c++·算法·leetcode·宽度优先
一条大祥脚11 天前
ABC460贪心|多源BFS|数论|计数|线段树|树的直径
算法·宽度优先
仍然.13 天前
算法题目---BFS解决FloodFill算法问题
算法·宽度优先
txzrxz15 天前
广度优先搜索详解(BFS)
算法·宽度优先
山楂树の21 天前
广度优先搜索 (BFS)
算法·广度优先·宽度优先
枫叶林FYL22 天前
Tree-of-Thought (ToT) 架构:BFS/DFS搜索策略、价值函数评估、剪枝机制实现
深度优先·剪枝·宽度优先
浅念-24 天前
LeetCode刷题专题:FloodFill泛滥填充算法剖析
数据结构·算法·leetcode·职场和发展·深度优先·宽度优先
山峰哥1 个月前
从Explain到SQL优化:一次生产环境慢查询的完整调优复盘
大数据·数据库·sql·性能优化·深度优先·宽度优先
汉克老师1 个月前
GESP6级C++考试语法知识(二十七、广度优先搜索(二、二维BFS))
c++·算法·图论·宽度优先·广度优先搜索·gesp6级·gesp六级