[CodeSignal] Arcade - Island of Knowledge (25) MineSweeper

김지원·2022년 5월 24일
0
post-custom-banner

📄 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

matrix = [[true, false, false],
          [false, true, false],
          [false, false, false]]

the output should be

solution(matrix) = [[1, 2, 1],
                       [2, 1, 1],
                       [1, 1, 1]]

Check out the image below for better understanding:

📌 Input/Output

  • [execution time limit] 4 seconds (py3)

  • [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.

💭 How to solve

Below is the neighbor cells

  • count mine(which is True in the array) in the neighbor cells.
  • you have to check if the cell is valid one.

💻 My Submission

def solution(matrix):
    answer=[]
    r, c=len(matrix),len(matrix[0])
    for i in range(r):
        ans=[]
        for j in range(c):
            mine=0
            adjacent=(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)
            for x, y in adjacent:
                if x<0 or y<0 or x>=r or y>=c:
                    continue
                if matrix[x][y]:
                    mine+=1
            ans.append(mine)
        answer.append(ans)
    return answer

Reference
https://app.codesignal.com/arcade/intro/level-5/ZMR5n7vJbexnLrgaM

profile
Make your lives Extraordinary!
post-custom-banner

1개의 댓글

comment-user-thumbnail
2024년 9월 15일

Throughout the tournament, Jake faced a diverse array of opponents and challenging hands. His ability to adapt his strategies and remain focused under pressure proved invaluable. As the tournament progressed, Jake’s tactical https://richcasinoau.com/ play and psychological insights allowed him to outmaneuver his competitors. By the time he reached the final table, his skill and perseverance had earned him the top spot and the championship title. Jake’s success underscored the importance of experience, preparation, and resilience in the pursuit of gambling excellence.

답글 달기