-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.py
More file actions
21 lines (19 loc) · 858 Bytes
/
minesweeper.py
File metadata and controls
21 lines (19 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/minesweeper/
# Author: Miao Zhang
# Date: 2021-02-20
class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
r, c = click
dirs = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, -1), (1, 1), (-1, 1), (-1, -1)]
if 0 <= r < len(board) and 0 <= c < len(board[0]):
if board[r][c] == 'M':
board[r][c] = 'X'
elif board[r][c] == 'E':
n = sum(board[r + dx][c + dy] == 'M' for dx, dy in dirs if 0 <= r + dx < len(board) and 0 <= c + dy < len(board[0]))
board[r][c] = str(n if n else 'B')
if not n:
for dx, dy in dirs:
self.updateBoard(board, [r + dx, c + dy])
return board