Challenge description
Let's call a two-dimentional, rectangular array (meaning its every subarray has the same length), a grid. Every unit of a grid is either an empty space or a border. In a grid of characters, empty space is represented by a single whitespace; any other character is treated as a border. Sample grids (+
's, |
's and -
's added for readability - they are not part of the grid):
+----+
| |
| |
| |
| |
| |
+----+ an empty 4x5 grid
+------+
| |
| # |
| # |
+------+ a 6x3 grid with 2 borders
+----------+
| |
| |
| ##### |
| # # |
| ## # <------ enclosed area
| # # |
| ###### |
| |
+----------+ a 10x8 grid with an enclosed area
Given a 2D grid and a pair of coordinates, fill the enclosed area surrounding the point represented by the coordinates.
Sample inputs / outputs
1)
0 0
+----------+ +----------+
| | |XXXXXXXXXX|
| | -> |XXXXXXXXXX|
| | |XXXXXXXXXX|
+----------+ +----------+
2)
6 5
+-----------------+ +-----------------+
| | | |
| | | |
| ######## | | ######## |
| # # | | #XXXXXXX# |
| # #### | | #XXXX#### |
| # # | | #XXXX# |
| # # | -> | #XXXX# |
| # # | | #XXXX# |
| #### | | #### |
| | | |
| | | |
+-----------------+ +-----------------+
3)
4 6
+-----------------+ +-----------------+
| | |XXXXXXXXXXXXXXXXX|
| #### | |XXXX####XXXXXXXXX|
| # # | -> |XXX# #XXXXXXXX|
| #### | |XXXX####XXXXXXXXX|
| | |XXXXXXXXXXXXXXXXX|
+-----------------+ +-----------------+
4)
4 5
+-----------------+ +-----------------+ +-----------------+
| | | | | |
| | | | | |
| #### | | #### | | XXXX |
| #### | -> | #### | or | XXXX |
| #### | | #### | | XXXX |
| | | | | |
+-----------------+ +-----------------+ +-----------------+
5)
2 6
+----------------+ +----------------+
| | |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
| | -> |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
|BBBBBBBBBBBBBBBB| |BBBBBBBBBBBBBBBB|
| | | |
| | | |
+----------------+ +----------------+
Notes
An empty grid is considered enclosed, i.e. borders are implicitly situated along the edges of the grid as well (see examples 1. and 5.),
A corner of an enclosed area doesn't need to be L-shaped. The following two areas are therefore equivalent:
#### ##
# # # #
# # == # #
# # # #
#### ##
If a unit under the coordinates happens to be a border you can either leave the grid unchanged (as in example 4.) or treat it as an empty space,
You can choose any character for filler / empty space as long as you include this information in the submission,
If using a type other than
char
suits your purposes better, you can useints
(0
for empty space,1
for border) orbooleans
(true
andfalse
respectively) or any other type - just make sure to include this information in your submission,Coordinates used in the examples above are 0-indexed
(row, column)
coordinates, as it's more convenient for two-dimensional array. If you want to use(column, row)
(cartesian) system and/or non-0-indexed coordinates, specify it in your submission.If you don't know where to start, check out Wikipedia article on flood fill
Remember that this is a code-golf challenge, so make your code as short as possible!