zoukankan      html  css  js  c++  java
  • 1189. 扫雷游戏

    1189. 扫雷游戏

    中文English

    让我们一起来玩扫雷游戏!

    给定一个代表游戏板的二维字符矩阵。 'M' 代表一个未挖出的地雷,'E' 代表一个未挖出的空方块,'B' 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的已挖出的空白方块,数字('1' 到 '8')表示有多少地雷与这块已挖出的方块相邻,'X' 则表示一个已挖出的地雷。

    现在给出在所有未挖出的方块中('M'或者'E')的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板:

    1. 如果一个地雷('M')被挖出,游戏就结束了- 把它改为 'X'。
    2. 如果一个没有相邻地雷的空方块('E')被挖出,修改它为('B'),并且所有和其相邻的方块都应该被递归地揭露。
    3. 如果一个至少与一个地雷相邻的空方块('E')被挖出,修改它为数字('1'到'8'),表示相邻地雷的数量。
    4. 如果在此次点击中,若无更多方块可被揭露,则返回面板。

    样例

    样例 1:

    输入: board = ["EEEEE","EEMEE","EEEEE","EEEEE"], Click : [3,0]
    输出: ["B1E1B","B1M1B","B111B","BBBBB"]
    

    样例 2:

    输入: board = ["B1E1B","B1M1B", "B111B","BBBBB"], Click : [1,2]
    输出: ["B1E1B","B1X1B","B111B","BBBBB"]
    

    注意事项

    1.输入矩阵的宽和高的范围为 [1,50]。
    2.点击的位置只能是未被挖出的方块 ('M' 或者 'E'),这也意味着面板至少包含一个可点击的方块。
    3.输入面板不会是游戏结束的状态(即有地雷已被挖出)。
    4.简单起见,未提及的规则在这个问题中可被忽略。例如,当游戏结束时你不需要挖出所有地雷,考虑所有你可能赢得游戏或标记方块的情况。

    BFS写法
    class Solution:
        """
        @param board: a board
        @param click: the position
        @return: the new board
        """
        def updateBoard(self, board, click):
            # Write your code here
            #bfs写法,如果是数字或者是M的话,则说明是不能再挖下去。如果是B的话,则可以加入队列继续挖出
            queue = [click]
            m, n = len(board), len(board[0])
            result = self.bfs(board, queue, m, n)
    
            return result
    
        def bfs(self, board, queue, m, n):
    
            while queue:
                array = queue.pop()
                x, y = array[0], array[1]
                
                #否则的话
                if board[x][y] == 'M':
                    board[x] = board[x][: y] + 'X' + board[x][y + 1: ]
                    continue
                
                #分情况考虑
                if board[x][y] == 'E':
                    res = self.isSatified(board, x, y, m, n)
                    if res:
                        board[x] = board[x][: y] + res + board[x][y + 1: ]
                    else:
                        board[x] = board[x][: y] + 'B' + board[x][y + 1: ]
                    
                        #只要是E就需要appeend
                        directions = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
                        for direction in directions:
                            new_x, new_y = x + direction[0], y + direction[1]
                            if new_x < 0 or new_x > m - 1 or new_y < 0 or new_y > n - 1:
                                continue 
    
                            if board[new_x][new_y] == 'E':
                                queue.append([new_x, new_y])
    
            return board
    
        #判断四周是否是雷
        def isSatified(self, board, x, y, m, n):
            directions = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
    
            count = 0
            for direction in directions:
                x1, y1 = direction[0] + x, direction[1] + y
    
                if x1 < 0 or x1 > m - 1 or y1 < 0 or y1 > n - 1:
                    continue
    
                #判断是否是雷
                if board[x1][y1] == 'M':
                    count += 1
            
            return str(count) if count != 0 else False

    DFS写法

    #DFS写法
    class Solution:
        """
        @param board: a board
        @param click: the position
        @return: the new board
        """
        def updateBoard(self, board, click):
            # Write your code here
            directions = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1], [0, 0]]
            m, n = len(board), len(board[0])
            self.dfs(board, m, n, click[0], click[1], directions)
    
            return board
    
        def dfs(self, board, m, n, x, y, directions):
            #如果是M的话,直接返回即可
            if board[x][y] == 'M': 
                board[x] = board[x][: y] + 'X' + board[x][y + 1: ]
                return 
            #如果是E的话, 需要判断周围存在多少个雷,如果是E则继续递归,否则continue
            for direction in directions:
                new_x, new_y = x + direction[0], y + direction[1]
                
                if 0 <= new_x < m and 0 <= new_y < n and board[new_x][new_y] == 'E':
                    res = self.getAround(board, m, n, x, y, directions)
                    if res:
                        board[x] = board[x][: y] + res + board[x][y + 1: ]
                        continue
                    board[x] = board[x][: y] + 'B' + board[x][y + 1: ]
                    self.dfs(board, m, n, new_x, new_y, directions)
            
        def getAround(self, board, m, n, x, y, directions):
            count = 0
    
            for direction in directions:
                new_x, new_y = direction[0] + x, direction[1] + y
    
                if 0 <= new_x < m and 0 <= new_y < n and board[new_x][new_y] == 'M':
                    count += 1 
            
            return str(count)  if count else False

    DFS(稍作调整)

    #DFS写法
    class Solution:
        """
        @param board: a board
        @param click: the position
        @return: the new board
        """
        def updateBoard(self, board, click):
            # Write your code here
            directions = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
            m, n = len(board), len(board[0])
            self.dfs(board, m, n, click[0], click[1], directions)
    
            return board
    
        def dfs(self, board, m, n, x, y, directions):
            #如果是M的话,直接返回即可
            if board[x][y] == 'M': 
                board[x] = board[x][: y] + 'X' + board[x][y + 1: ]
                return 
    
            res = self.getAround(board, m, n, x, y, directions)
            if res:
                board[x] = board[x][: y] + res + board[x][y + 1: ]
            else:
                board[x] = board[x][: y] + 'B' + board[x][y + 1: ]
                    
                #如果不是数字的话,则说明可以继续四周递归下去, res是计算四周雷的个数
                for direction in directions:
                    new_x, new_y = x + direction[0], y + direction[1]
                    
                    if 0 <= new_x < m and 0 <= new_y < n and board[new_x][new_y] == 'E':
                        self.dfs(board, m, n, new_x, new_y, directions)
            
        def getAround(self, board, m, n, x, y, directions):
            count = 0
    
            for direction in directions:
                new_x, new_y = direction[0] + x, direction[1] + y
    
                if 0 <= new_x < m and 0 <= new_y < n and board[new_x][new_y] == 'M':
                    count += 1 
            
            return str(count) if count else False
  • 相关阅读:
    sql 在日期范围内搜索
    js 处理日期时间字符串显示的方法
    matlab练习程序(并行计算)
    C++程序运行时间
    matlab练习程序(KNN,K最邻近分类法)
    多媒体指令(像素处理)
    ubuntu启动/重启/停止apache
    matlab练习程序(matlab调用c/c++)
    我的vim设置
    matlab练习程序(c/c++调用matlab<engine>)
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/13983288.html
Copyright © 2011-2022 走看看