Skip to main content
Changed "working" list to "unvisited" list and "filled" list to "visited" list to keep names consistent
Source Link

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from workingunvisited list
   add current element to filledvisited list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from unvisited list
   add current element to visited list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

deleted 101 characters in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

This is non-recursive and so won't cause the potential slowdowns that a recursive approach might.

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

This is non-recursive and so won't cause the potential slowdowns that a recursive approach might.

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

deleted 1 character in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare filledvisited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

This is non-recursive and so won't cause the potential slowdowns that a recursive approach might.

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare filled list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          add neighbour to unvisited list
   remove current element from unvisited list

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode:

declare visited list //the results you want
declare unvisited list

add current element (where red dot is) to unvisited list

while unvisited list not empty
   get current element from working list
   add current element to filled list
   for all (8) neighbours of current element
       if neighbour is green
          if neighbour is not in visited list //be sure not to process same ones again!
              add neighbour to unvisited list
   remove current element from unvisited list

The way I'd do this is using a set (or hashtable if set not available); as these structures don't allow duplicates, you don't have to do the if neighbour is not in visited list in your own code.

This is non-recursive and so won't cause the potential slowdowns that a recursive approach might.

deleted 1 character in body
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
Loading
Source Link
Engineer
  • 30.4k
  • 4
  • 76
  • 124
Loading