順便推坑一下踩地雷 連結
(https://minesweeper.online/tw/game/1390423794)
Description
In the popular Minesweeper game you have a board with some mines and those cells that don’t contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.
Example
For
|
|
the output should be
|
|
Input/Output
[execution time limit] 4 seconds (rb)
[input] array.array.boolean matrix
A non-empty rectangular matrix consisting of boolean values -
true
if the corresponding cell contains a mine,false
otherwise.Guaranteed constraints:
2 ≤ matrix.length ≤ 100
,2 ≤ matrix[0].length ≤ 100
.[output] array.array.integer
Rectangular
matrix
of the same size as matrix each cell of which contains an integer equal to the number of mines in the neighboring cells. Two cells are called neighboring if they share at least one corner.
給定一個二維陣列,元素表示該格是否為地雷,要回傳一個二維陣列,元素為周圍的地雷數(踩地雷遊戲裡的數字)
First try
格子內的數字 = 周遭 8 格的地雷數
因此找出上下左右的 index,將範圍內地雷數(true)加總
因為會加到自己,所以如果本身格子是地雷的話,上面算出的數字 − 1
如上 A: 1, B: 2, C: 3, D:1
|
|
整理一下
上面對陣列 slice 切出往外一圈的範圍
不過我們也可以直接讀取周圍 8 格的資訊
|
|
附註:有空做一個踩地雷網站玩玩看 XD