A knight fill is a flood fill using the connectivity of the knight chess piece. Specifically:
1 1
1 1
0
1 1
1 1
(0 is the initial point, 1s show the connected cells)
Challenge
Given a 2D grid of spaces and walls, and an initial location, perform a knight-fill on the grid. Shortest code wins.
Rules
You may take input and produce output in any format you like (image, string, array, whatever). You may take the initial location as part of the input grid or as a separate coordinate. For the purpose of this explanation, the following format will be used:
######## # = wall ######## x = initial location ## x ## ## ## ######## ## ## ######## ########Output is a copy of the input grid with the knight-fill result added
Your fill must not be in the same "colour" as the space or walls, but can be the same as the initial location marker. For example given the image above, a valid output would be:
######## # = wall ######## @ = fill (could also have been x) ## @ @## ## @ @## ######## ##@ @ ## ######## ########You may assume that the input grid will always contain a 2-cell wall on all sides
- You may assume that the initial location will never be inside a wall
- You may assume that the grid will never be larger than 1000x1000
- Builtins are fine
- Shortest code (in bytes) wins
Test Cases
In all test cases, # denotes a wall, denotes empty space, and x denotes the initial location of the fill. @ denotes the output fill.
Input 1:
########
########
## x ##
## ##
########
## ##
########
########
Output 1:
########
########
## @ @##
## @ @##
########
##@ @ ##
########
########
Input 2:
############
############
## ## x##
## ## ##
##### ##
## ##
############
############
Output 2:
############
############
## ##@@@@@##
##@##@@@@@##
#####@@@@@##
## @@@@@@@##
############
############
Input 3:
####################
####################
## ## ##
## ## ##
## ## ######## ##
## ## ######## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ######## ##
## ## ######## ##
## ## ## ##
## ## x## ##
## ############ ##
## ############ ##
## ##
## ##
####################
####################
Output 3:
####################
####################
##@@##@@@@@@@@@@@@##
##@@##@@@@@@@@@@@@##
##@@##@@########@@##
##@@##@@########@@##
##@@##@@## ##@@##
##@@##@@## ##@@##
##@@##@@## ##@@##
##@@##@@## ##@@##
##@@##@@########@@##
##@@##@@########@@##
##@@##@@@@@@@@##@@##
##@@##@@@@@@@@##@@##
##@@############@@##
##@@############@@##
##@@@@@@@@@@@@@@@@##
##@@@@@@@@@@@@@@@@##
####################
####################
Input 4:
################
################
## ###
## x ###
## ####### ###
## ####### ###
## ## ## ###
## ## ## ###
## ## ## ###
## ######## ##
## ######## ##
## ## ##
## ## ##
################
################
Output 4:
################
################
## @ @ ###
## @ @ @ ###
## ####### ###
##@ ####### @###
## ## ## ###
## @## ##@ ###
## ## ## ###
##@ ########@ ##
## ######## ##
## @ @ ## @##
## @ @## ##
################
################
Input 5:
##############
##############
## ###
## ###
## ###
## ### ###
## #x# ###
## ### ###
## ###
## ###
## ###
##############
##############
Output 5:
##############
##############
##@@@@@@@@@###
##@@@@@@@@@###
##@@@@@@@@@###
##@@@###@@@###
##@@@#@#@@@###
##@@@###@@@###
##@@@@@@@@@###
##@@@@@@@@@###
##@@@@@@@@@###
##############
##############
-
\$\begingroup\$ Somewhat related. \$\endgroup\$Martin Ender– Martin Ender2017年06月24日 14:20:32 +00:00Commented Jun 24, 2017 at 14:20
4 Answers 4
Octave, 73 bytes
function a=F(s,a)do;b=a;until(a=~s&imdilate(a,de2bi(")0#0)"-31)))==b;a+=s
*Some changes applied to run in rextester.
A function that takes a 2d array of 0 & 2 as wall and an array of 0 & 1 as initial location and outputs an array of 0 & 1 & 2.
-
\$\begingroup\$ Looks good, but doesn't this need
pkg load ...when run outside the test framework? Ifimdilate&de2biare available without explicit imports then that's fine. \$\endgroup\$Dave– Dave2017年06月28日 18:28:47 +00:00Commented Jun 28, 2017 at 18:28 -
\$\begingroup\$ @Dave In previous versions of octave ,including the version installed in tio, it was possible to install a package so that it could load automatically but now I noticed that this feature is removed from octave! please see this. \$\endgroup\$rahnema1– rahnema12017年06月28日 18:39:38 +00:00Commented Jun 28, 2017 at 18:39
-
\$\begingroup\$ Fair enough. As long as you're targeting a version before
-autowas removed it's no problem, and I'm guessing this answer doesn't use any new features. \$\endgroup\$Dave– Dave2017年06月28日 18:47:52 +00:00Commented Jun 28, 2017 at 18:47
JavaScript (ES6), 116 bytes
f=(s,l=s.search`
`,t=s.replace(eval(`/(x| )([^]{${l-2}}(....)?|[^]{${l+l}}(..)?)(?!\1円)[x ]/`),'x2ドルx'))=>s==t?s:f(t)
v=(s,l=s.search`
`)=>!/^(#+)\n1円\n[^]*x[^]*\n1円\n1円$/.test(s)|s.split`
`.some(s=>s.length-l|!/^##.+##$/.test(s))&&`Invalid Input`
textarea{font-family:monospace}
<textarea rows=11 cols=33 oninput=o.value=v(this.value)||f(this.value)></textarea><textarea rows=11 cols=33 id=o reaodnly></textarea>
Based on my answer to Detect Failing Castles. Fills using xs.
-
\$\begingroup\$ Can you add a test snippet/link? \$\endgroup\$0xffcourse– 0xffcourse2017年06月25日 01:38:20 +00:00Commented Jun 25, 2017 at 1:38
Python 3, (削除) 394 387 381 356 352 347 319 313 154 (削除ここまで)139 bytes
- 154 bytes after only counting the core function and not the function concerning I/O formatting
- saved 7 bytes: thanks to @Jacoblaw and @Mr.Xcoder:
except:0 - saved 28 bytes!!!: Thanks to @ovs: got rid of
try: exceptblock and several other golf - Thanks to @Dave for the beautiful test module.
- saved 6 bytes:
g[(a,b)]as justg[a,b] - @nore saved 15 bytes!!!:
def x(g,a,b,m):
if(a,b)in g and"!">g[a,b]or m:
g[a,b]="@"
for i in 1,2,-1,-2:
for j in 3-abs(i),abs(i)-3:g=x(g,a+i,b+j,0)
return g
-
1\$\begingroup\$ can you do
except:passinstead? \$\endgroup\$jacoblaw– jacoblaw2017年06月24日 18:01:34 +00:00Commented Jun 24, 2017 at 18:01 -
1\$\begingroup\$ I am pretty sure that this can be heavily golfed \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年06月24日 18:01:48 +00:00Commented Jun 24, 2017 at 18:01
-
2\$\begingroup\$ @jacoblaw even better:
except:0\$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年06月24日 18:02:13 +00:00Commented Jun 24, 2017 at 18:02 -
1
-
1\$\begingroup\$ Here's a slightly easier-to-test version of the TiO: Try it online! \$\endgroup\$Dave– Dave2017年06月24日 21:27:55 +00:00Commented Jun 24, 2017 at 21:27
Mathematica, 117 bytes
The usual story: powerful built-ins but long names...
HighlightGraph[g,ConnectedComponents[h=Subgraph[g=KnightTourGraph@@Dimensions@#,Flatten@#~Position~1],#2]~Prepend~h]&
Try it at the Wolfram sandbox!
It takes two inputs: first is the input grid as an array of 0s (for the walls) and 1s (for spaces), then a single integer for the starting position, found by numbering the grid along the rows from top to bottom, e.g.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 ...
You can call the function like HighlightGraph[...~Prepend~h]&[{{0,0,...,0}, {0,0,...,0}, ..., {0,0,...,0}}, 20].
The KnightTourGraph function constructs a graph with vertices corresponding to positions in the grid and edges corresponding to valid knight moves, then we take the Subgraph of the vertices that aren't walls, and find the ConnectedComponents of the starting vertex. The output is a graph (shown rotated 90o anticlockwise) with the non-wall vertices highlighted red, and the filled vertices highlighted yellow. For example, for the first test case, the output looks like:
-
\$\begingroup\$ Well this certainly looks like the hardest to test! Could you add an example of how to invoke it in the sandbox, for those of us who haven't touched Mathematica since our university days? My attempts of
f=...f[{0,...,0;0,...,0}, 19]and similar have failed miserably. \$\endgroup\$Dave– Dave2017年06月28日 18:44:38 +00:00Commented Jun 28, 2017 at 18:44 -
\$\begingroup\$ @Dave, you can invoke the function with
HighlightGraph[g,ConnectedComponents[h=Subgraph[g=KnightTourGraph@@Dimensions@#,Flatten@#~Position~1],#2]~Prepend~h]&[{{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,1,1,1,1,0,0},{0,0,1,1,1,1,0,0},{0,0,0,0,0,0,0,0},{0,0,1,1,1,1,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}},20](for the first test case). I've edited that into the question — sorry it wasn't there to begin with! \$\endgroup\$Not a tree– Not a tree2017年06月28日 23:19:44 +00:00Commented Jun 28, 2017 at 23:19
Explore related questions
See similar questions with these tags.