If r+dr in range(len(board)) and c+dc in range(len(s)): Result = for c in row] for row in board] # initializeįor r,s in enumerate(board): # go through board rowsįor c,m in enumerate(s): # co through row's columnsįor dr,dc in if m="X" else offsets: # neighbours of "O" cells Then a nested loop on each position can go through the offsets to add 1 to the 'zero' cells when the neighbouring position is in range of the board and contains an "X": board = ["OOOXXXOXX", You can initialize a result matrix with a zero on "O" cells and "X" on the mine positions. I was given 15 minutes to solve this in a coding challenge, and still can't figure out for the life of me how someone would have approached this.Īn easy way to get to the adjacent positions is to prepare a list of offsets for the 8 neighbouring cells based on the row and column numbers. I believe there must be a better solution in terms of space-time complexity and just in general. Now here is my working solution: def minesweeper(array): If any of these cells contain a mine, the cell we are checking it for becomes the NUMBER of mines we have just counted. This means we need to check at 8 spots for each cell: Top left, Top Middle, Top Right, Middle Right, Middle Left, Bottom Left, Bottom Middle, and Bottom Right. Rules are super simple: We take as input a grid of where the mines are, and we output a grid where each cell represents the number of mines explicitly around it.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |