leetcode 529. Minesweeper

Let's play the minesweeper game (Wikipedia, online game)!

You are given a 2D char matrix representing the game board. 'M'represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.

Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:

  1. If a mine ('M') is revealed, then the game is over - change it to 'X'.
  2. If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealedsquares should be revealed recursively.
  3. If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
  4. Return the board when no more squares will be revealed.

Example 1:

Input:

[['E', 'E', 'E', 'E', 'E'],
['E', 'E', 'M', 'E', 'E'],
['E', 'E', 'E', 'E', 'E'],
['E', 'E', 'E', 'E', 'E']]

Click : [3,0]

Output:

[['B', '1', 'E', '1', 'B'],
['B', '1', 'M', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']]

Explanation:

image

Example 2:

Input:

[['B', '1', 'E', '1', 'B'],
['B', '1', 'M', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']]

Click : [1,2]

Output:

[['B', '1', 'E', '1', 'B'],
['B', '1', 'X', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']]

Explanation:

image

Note:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  3. The input board won't be a stage when game is over (some mines have been revealed).
  4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

這個是掃雷游戲,需要注意上面的四條規(guī)則失球,可以用DFS求解,在給定的位置上點擊時,如果點到了地雷携丁,那就改為‘X’,直接返回整個數(shù)組就行支鸡,如果沒有點到地雷剧蚣,根據(jù)上述的規(guī)則改相應(yīng)的字母。還有吗货,如果附近有地雷泳唠,那就根據(jù)周圍地雷的數(shù)量,把自己的位置改為這個數(shù)字的str類型就行宙搬,并且在周圍沒有地雷的位置上繼續(xù)遞歸笨腥,直到?jīng)]有更多的點被揭露,返回整個方塊勇垛。

注意:

  • 在Example 1中脖母,在(0,3)位置上之所有沒有揭露,那是因為它的線面有地雷闲孤,左右邊是有'1'谆级,而且這個'1'所在的方塊中,是不能繼續(xù)遞歸下去的讼积。所以最后(0,3)這個位置沒有被揭露了肥照。所以DFS就油然而生了。
  • 這個方向是有八個方向的勤众。

python3代碼:

class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        lenr, lenc = len(board), len(board[0])
        if lenr == 0 or lenc == 0:
            return board
        
        sr, sc = click[0], click[1]
        if board[sr][sc] == 'M':
            board[sr][sc] = 'X'
            return board
        
        visited = [[0 for i in range(lenc + 1)] for j in range(lenr + 1)]
        dx = [1, 1, -1, -1, 0, 0, -1, 1]
        dy = [1, 0, -1, 0, 1, -1, 1, -1]
        
        def dfs(r, c):
            if board[r][c] == 'M' or visited[r][c] == 1:
                return
            visited[r][c] = 1
            num_mine = 0
            for i in range(8):
                x = r + dx[i]
                y = c + dy[i]
                if 0 <= x < lenr and 0 <= y < lenc and board[x][y] == 'M':
                    num_mine += 1
            if num_mine > 0:
                board[r][c] = str(num_mine)
            else:
                board[r][c] = 'B'
                
                for i in range(8):
                    x = r + dx[i]
                    y = c + dy[i]
                    
                    if 0 <= x < lenr and 0 <= y < lenc and visited[x][y] == 0:
                        dfs(x, y)
        dfs(sr, sc)
        return board
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末建峭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子决摧,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掌桩,死亡現(xiàn)場離奇詭異边锁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)波岛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門茅坛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人则拷,你說我怎么就攤上這事贡蓖。” “怎么了煌茬?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵斥铺,是天一觀的道長。 經(jīng)常有香客問我坛善,道長晾蜘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任眠屎,我火速辦了婚禮剔交,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘改衩。我一直安慰自己岖常,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布葫督。 她就那樣靜靜地躺著竭鞍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪候衍。 梳的紋絲不亂的頭發(fā)上笼蛛,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音蛉鹿,去河邊找鬼滨砍。 笑死,一個胖子當(dāng)著我的面吹牛妖异,可吹牛的內(nèi)容都是我干的惋戏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼他膳,長吁一口氣:“原來是場噩夢啊……” “哼响逢!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起棕孙,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤舔亭,失蹤者是張志新(化名)和其女友劉穎些膨,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體钦铺,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡订雾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了矛洞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片洼哎。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖沼本,靈堂內(nèi)的尸體忽然破棺而出噩峦,到底是詐尸還是另有隱情,我是刑警寧澤抽兆,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布识补,位于F島的核電站,受9級特大地震影響郊丛,放射性物質(zhì)發(fā)生泄漏李请。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一厉熟、第九天 我趴在偏房一處隱蔽的房頂上張望导盅。 院中可真熱鬧,春花似錦揍瑟、人聲如沸白翻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滤馍。三九已至,卻和暖如春底循,著一層夾襖步出監(jiān)牢的瞬間巢株,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工熙涤, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留阁苞,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓祠挫,卻偏偏與公主長得像那槽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子等舔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容