leecode 广度优先搜索 BFS

1222 可攻击国王的皇后

class Solution(object):
    def queensAttacktheKing(self, queens, king):
        """
        :type queens: List[List[int]]
        :type king: List[int]
        :rtype: List[List[int]]
        """
        queens = set(tuple(i) for i in queens)
        movies = [(-1,0), (1,0), (-1,1), (1,-1), (0,-1), (0,1), (1,1), (-1,-1)]
        (x, y) = king
        stash = []
        ans = []
        for dx,dy in movies:
            if -1<x+dx<8 and -1<y+dy<8:
                if (x+dx, y+dy) in queens:
                    ans.append([x+dx, y+dy])
                else:
                    stash.append([x+dx, y+dy, dx, dy])
        while stash:
            x, y, dx, dy = stash.pop(0)
            if -1<x+dx<8 and -1<y+dy<8:
                if (x+dx, y+dy) in queens:
                    ans.append([x+dx, y+dy])
                else:
                    stash.append([x+dx, y+dy, dx, dy])
        return ans

102 二叉树的层次遍历

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root: return []
        ans = []
        ans.append([root.val])
        stash = []
        stash.append([root])
        while stash:
            q = []
            t = []
            rt = stash.pop(0)
            for r in rt:
                if r.left: 
                    q.append(r.left.val)
                    t.append(r.left)
                if r.right: 
                    q.append(r.right.val)
                    t.append(r.right)
            if q: ans.append(q)
            if t: stash.append(t)
        return ans

107 二叉树层次遍历II

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root: return []
        ans = []
        ans.append([root.val])
        stash = []
        stash.append([root])
        while stash:
            q = []
            t = []
            rt = stash.pop(0)
            for r in rt:
                if r.left: 
                    q.append(r.left.val)
                    t.append(r.left)
                if r.right: 
                    q.append(r.right.val)
                    t.append(r.right)
            if q: ans.append(q)
            if t: stash.append(t)
        return ans[::-1]

127 单词接龙

class Solution(object):
    # 本题采用广度遍历方法
    def ladderLength(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: List[str]
        :rtype: int
        """
        word_set = set(wordList)
        cur_word = []
        cur_word.append(beginWord)
        ans = 1
        stash = []
        while cur_word:
            for word in cur_word:
                if word == endWord:
                    return ans
                for i in range(len(word)):
                    for s in "abcdefghijklmnopqrstuvwxyz":
                        new_word = word[:i]+s+word[i+1:]
                        if new_word in word_set:
                            word_set.remove(new_word)
                            stash.append(new_word)
            ans += 1
            cur_word = stash
            stash = []
        return 0

1091 二进制矩阵中的最短路径

class Solution(object):
    def shortestPathBinaryMatrix(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        n, m = len(grid), len(grid[0])
        if n==1 and m==1:
            return 1
        if grid[0][0]==1 or grid[n-1][m-1]==1:
            return -1
        ans = 1
        q = []
        stash = []
        q.append([0, 0])
        visted = [[0] * m for _ in range(n)]
        visted[0][0] = 1
        movies = [(-1,0), (1,0), (-1,1), (1,-1), (0,-1), (0,1), (1,1), (-1,-1)]
        while q:
            x, y = q.pop(0)
            ans = visted[x][y]
            ans += 1
            for dx, dy in movies:
                if x+dx == n - 1 and y+dy == m - 1:
                    return ans
                if -1<x+dx<n and -1<y+dy<m:
                    if 0==visted[x+dx][y+dy] and grid[x+dx][y+dy] != 1:
                        stash.append([x+dx, y+dy])
                        visted[x+dx][y+dy] = ans
            if len(q)==0:
                q=stash
                stash=[]
        return -1

猜你喜欢

转载自blog.csdn.net/qq_40006058/article/details/102637753