Last active
March 31, 2018 19:05
-
-
Save fabionoth/c029de6ba07e6b268eb0a3d1ad66d050 to your computer and use it in GitHub Desktop.
Challenge Spiral Matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def challenge(n, counter, x, y, d): | |
if(DEBUG): | |
print 'n={}, counter={}, [{},{}], direction={}'.format(n, counter, x, y, d) | |
print n_array | |
if n_array[x, y] != 0: | |
return 1 | |
else : | |
n_array[x, y] = counter | |
# Right | |
if y + 1 < n and n_array[x, y + 1] == 0 and d == 0: | |
challenge(n, counter + 1, x, y + 1, d) | |
elif d == 0: | |
challenge(n, counter + 1, x + 1, y, d + 1) | |
# Down | |
if x + 1 < n and n_array[x + 1, y] == 0 and d == 1: | |
challenge(n, counter + 1, x + 1, y, d) | |
elif d == 1: | |
challenge(n, counter + 1, x, y - 1, d + 1) | |
# Left | |
if y - 1 >= 0 and n_array[x, y -1] == 0 and d == 2: | |
challenge(n, counter + 1, x, y -1, d) | |
elif d == 2: | |
challenge(n, counter + 1, x - 1, y, d + 1) | |
# Top | |
if x - 1 >= 0 and n_array[x - 1, y] == 0 and d == 3: | |
challenge(n, counter + 1, x - 1, y, d) | |
elif d == 3: | |
challenge(n - 1, counter + 1, x, y + 1, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment