Ejemplo n.º 1
0
    def flood_fill(self, board, pos):
        d = Direction()
        spaces_filled = 0
        empty = 0
        filled = 1
        b = deepcopy(board)
        q = [pos]  # 2. Add node to the end of Q.
        while len(q) > 0:  # 4. While Q is not empty:
            n = q.pop(
            )  # 5. Set n equal to the last element of Q, and remove last element from Q
            if b[n[0]][n[
                    1]] == empty:  # 8. If the color of n is equal to target-color:
                b[n[0]][n[
                    1]] = filled  # 9. Set the color of n to replacement-color.
                spaces_filled += 1
                q.append(d.get_new_coord(
                    n, Direction.North))  # 10. Add North node to end of Q.
                q.append(d.get_new_coord(
                    n, Direction.East))  # 11. Add East node to end of Q.
                q.append(d.get_new_coord(
                    n, Direction.South))  # 12. Add South node to end of Q.
                q.append(d.get_new_coord(
                    n, Direction.West))  # 13. Add West node to end of Q.

        return spaces_filled  # 14. Return.
Ejemplo n.º 2
0
 def flood_fill(self, board, pos):
     d = Direction()
     spaces_filled = 0
     empty = 0
     filled = 1
     b = deepcopy(board)
     q = [pos]               # 2. Add node to the end of Q.
     while len(q) > 0:       # 4. While Q is not empty: 
         n = q.pop()         # 5. Set n equal to the last element of Q, and remove last element from Q     
         if b[n[0]][n[1]] == empty:   # 8. If the color of n is equal to target-color:
             b[n[0]][n[1]] = filled   # 9. Set the color of n to replacement-color.
             spaces_filled += 1
             q.append(d.get_new_coord(n, Direction.North)) # 10. Add North node to end of Q.
             q.append(d.get_new_coord(n, Direction.East))  # 11. Add East node to end of Q.
             q.append(d.get_new_coord(n, Direction.South))  # 12. Add South node to end of Q.
             q.append(d.get_new_coord(n, Direction.West))   # 13. Add West node to end of Q.
             
     return spaces_filled    # 14. Return.