zoukankan      html  css  js  c++  java
  • 【leetcode】1559. Detect Cycles in 2D Grid

    题目如下:

    Given a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid.

    A cycle is a path of length 4 or more in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the same value of the current cell.

    Also, you cannot move to the cell that you visited in your last move. For example, the cycle (1, 1) -> (1, 2) -> (1, 1) is invalid because from (1, 2) we visited (1, 1) which was the last visited cell.

    Return true if any cycle of the same value exists in grid, otherwise, return false.

    Example 1:

    Input: grid = [["a","a","a","a"],["a","b","b","a"],["a","b","b","a"],["a","a","a","a"]]
    Output: true
    Explanation: There are two valid cycles shown in different colors in the image below:
    

    Example 2:

    Input: grid = [["c","c","c","a"],["c","d","c","c"],["c","c","e","c"],["f","c","c","c"]]
    Output: true
    Explanation: There is only one valid cycle highlighted in the image below:
    

    Example 3:

    Input: grid = [["a","b","b"],["b","z","b"],["b","b","a"]]
    Output: false

    Constraints:

    • m == grid.length
    • n == grid[i].length
    • 1 <= m <= 500
    • 1 <= n <= 500
    • grid consists only of lowercase English letters.

    解题思路:题目不难,利用dfs/bfs的思想遍历一遍就行了,遍历的过程中记录走过的节点,遇到重复就说明有环。有一点要注意,需要同时记录到达当前节点的上一个节点的位置,往下一个节点走的时候不能重复回到上一个节点,以免掉入死循环。

    代码如下:

    class Solution(object):
        def containsCycle(self, grid):
            """
            :type grid: List[List[str]]
            :rtype: bool
            """
            visit = {}
            for i in range(len(grid)):
                for j in range(len(grid[i])):
                    if (i,j) in visit:continue
                    visit[(i, j)] = 1
                    queue = [(None,None,i,j)]
                    while len(queue) > 0:
                        lx,ly,x,y = queue.pop(0)
                        direction = [(0,1),(0,-1),(1,0),(-1,0)]
                        for (x1,y1) in direction:
                            if x + x1 >= 0 and x + x1 < len(grid) and y + y1 >= 0 
                                    and y + y1 < len(grid[0]) and grid[x][y] == grid[x+x1][y+y1] 
                                and (lx != x + x1 or ly != y + y1):
                                if (x + x1,y+y1) in visit:
                                    return True
                                visit[(x+x1,y+y1)] = 1
                                queue.append((x,y,x+x1,y+y1))
            return False
  • 相关阅读:
    在win2003中发布部署vs2010b2写的mvc2网站
    安装blender2.5Alpha0
    Win7下虚拟机个人使用小结:Virtual PC,VMware和VirtualBox
    ASP.NET AJAX Control Toolkit Beta 0911 发布[再增两控件]
    Camtasia 6录屏时鼠标闪烁问题解决
    为XNA制做安装程序(四)WIX Toolset 3.0 for Visual Studio 2008
    Oracle EM 12c
    无题
    从徐汇到虹口
    近况
  • 原文地址:https://www.cnblogs.com/seyjs/p/14794225.html
Copyright © 2011-2022 走看看