Minesweeper

whitehousechef·2025년 10월 30일

absolutely retarded q with confusing logic but ez q

https://www.coditioning.com/app/learning/courses/tech_interview_prep/9/modules/10/submodules/90/units/10000015/challenge/10000015

initial

def updateBoard(board, click):
    row,col = len(board),len(board[0])
    if board[click[0]][click[1]]=="M":
        return board
    moves=[[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]

    def countMines(board_copy,x,y):
        count=0
        for move in moves:
            next_x,next_y=move[0]+x,move[1]+y
            if 0<=next_x<row and 0<=next_y<col and board_copy[next_x][next_y]=="M":
               count+=1
        return count

    queue=[click]
    while queue:
        cur_x,cur_y = queue.pop(0)
        for move in moves:
            next_x,next_y=move[0]+cur_x,move[1]+cur_y
            count_returned = countMines(board,next_x,next_y)
            if count_returned:
                board[next_x][next_y]=count_returned
            else:
                board[cur_x][cur_y] = "B"
                if 0 <= next_x < row and 0 <= next_y < col:
                    queue.append([next_x,next_y])
    return board

sol

No, this code has several issues. Let me point them out:

Problems:

1. Missing Mine Reveal

if board[click[0]][click[1]]=="M":
    return board  # ❌ Should change to 'X' first!

Should be:

if board[click[0]][click[1]]=="M":
    board[click[0]][click[1]] = 'X'
    return board

2. Not Processing the Clicked Cell First

You add click to the queue but never process it before the loop. You need to handle the initial clicked cell.

3. Wrong Logic in the Main Loop

for move in moves:
    next_x,next_y=move[0]+cur_x,move[1]+cur_y
    count_returned = countMines(board,next_x,next_y)  # ❌ Checking neighbors, not current!

You're counting mines for the neighbor cells but you should count mines for the current cell being processed.

4. Missing Boundary Check Before Counting

You call countMines(board,next_x,next_y) without checking if next_x, next_y is in bounds first.

5. Missing Check for Already Processed Cells

You don't check if a cell is 'E' before processing it. You might process the same cell multiple times or try to process already revealed cells.

6. Wrong Assignment Location

if count_returned:
    board[next_x][next_y]=count_returned  # ❌ Setting neighbor
else:
    board[cur_x][cur_y] = "B"  # ❌ Setting current only when neighbor has 0 mines?

This logic is backwards. You should set the current cell based on its own mine count, not its neighbor's count.

Corrected Version:

def updateBoard(board, click):
    row, col = len(board), len(board[0])
    
    # Rule 1: If clicked on mine, reveal it
    if board[click[0]][click[1]] == "M":
        board[click[0]][click[1]] = "X"
        return board
    
    moves = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
    
    def countMines(x, y):
        count = 0
        for move in moves:
            next_x, next_y = move[0] + x, move[1] + y
            if 0 <= next_x < row and 0 <= next_y < col and board[next_x][next_y] == "M":
                count += 1
        return count
    
    queue = [click]
    
    while queue:
        cur_x, cur_y = queue.pop(0)
        
        # Skip if already processed
        if board[cur_x][cur_y] != "E":
            continue
        
        # Count mines for CURRENT cell
        mine_count = countMines(cur_x, cur_y)
        
        if mine_count > 0:
            # Has adjacent mines: show count and stop
            board[cur_x][cur_y] = str(mine_count)
        else:
            # No adjacent mines: mark as B and add neighbors
            board[cur_x][cur_y] = "B"
            
            for move in moves:
                next_x, next_y = move[0] + cur_x, move[1] + cur_y
                if 0 <= next_x < row and 0 <= next_y < col and board[next_x][next_y] == "E":
                    queue.append([next_x, next_y])
    
    return board

Key Fixes:

  1. ✅ Change mine to 'X' when clicked
  2. ✅ Count mines for the current cell, not neighbors
  3. ✅ Check if current cell is 'E' before processing
  4. ✅ Only add 'E' neighbors to queue
  5. ✅ Set current cell based on its own mine count
  6. ✅ Proper boundary checks

0개의 댓글