Skip to content

DFS-1#1410

Open
nagasai67 wants to merge 1 commit intosuper30admin:masterfrom
nagasai67:master
Open

DFS-1#1410
nagasai67 wants to merge 1 commit intosuper30admin:masterfrom
nagasai67:master

Conversation

@nagasai67
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Flood Fill the image (01matrix.py)

It seems you have submitted code for the wrong problem. The problem is "Flood Fill", but your solution is for "01 Matrix". Please double-check the problem statement and requirements.

For the "Flood Fill" problem, you need to implement a BFS or DFS starting from the given pixel (sr, sc). The goal is to change the color of the starting pixel and all connected pixels (same original color) to the new color. Here are the steps you should follow:

  1. Check if the starting pixel already has the new color. If so, return the image immediately.
  2. Use a queue (for BFS) or a stack (for DFS) to traverse all pixels that are connected and have the same original color as the starting pixel.
  3. For each pixel, change its color to the new color and then check its four neighbors (up, down, left, right). If a neighbor is within bounds and has the original color, add it to the queue/stack.

Example code for flood fill in Python:

from collections import deque

class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
        if image[sr][sc] == color:
            return image
            
        rows, cols = len(image), len(image[0])
        original_color = image[sr][sc]
        q = deque()
        q.append((sr, sc))
        image[sr][sc] = color
        
        directions = [(1,0), (-1,0), (0,1), (0,-1)]
        
        while q:
            r, c = q.popleft()
            for dr, dc in directions:
                nr, nc = r + dr, c + dc
                if 0 <= nr < rows and 0 <= nc < cols and image[nr][nc] == original_color:
                    image[nr][nc] = color
                    q.append((nr, nc))
                    
        return image

Please note the differences:

  • We start from (sr, sc) and change the color only if it's not already the target color.
  • We compare neighbors with the original color (not zero) and change them to the new color.
  • We don't need a separate distance matrix; we modify the image in place.

VERDICT: NEEDS_IMPROVEMENT


Nearest zero (floodfill.py)

It appears you have submitted a solution for the "Flood Fill" problem instead of the "Nearest Zero" problem. This is a critical mistake. Please ensure you are solving the correct problem.

For the "Nearest Zero" problem, you need to compute the distance from each cell to the nearest 0. The recommended approach is to use a multi-source BFS:

  1. Initialize a queue and add all cells with 0. Mark these as distance 0.
  2. For cells with 1, mark them as unvisited (e.g., with -1 or a large number).
  3. Process the queue: for each cell, check its neighbors. If a neighbor is unvisited, update its distance to the current distance + 1 and add it to the queue.

This approach efficiently computes the distances by propagating from the zeros outward.

Your code for Flood Fill is correct for that problem, but it does not address the Nearest Zero problem. Please review the problem statement carefully and implement the correct algorithm.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants