Last active
February 9, 2018 06:46
-
-
Save nkmrtty/0da815f786edbb8a1b93b690389af512 to your computer and use it in GitHub Desktop.
Lights Out
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 switch(cur_lights, pos, N, M): | |
new_lights = list(cur_lights) | |
row = pos // M | |
left = row * M | |
right = (row + 1) * M - 1 | |
# center | |
new_lights[pos] ^= 1 | |
# left | |
if left < pos <= right: | |
new_lights[pos - 1] ^= 1 | |
# right | |
if left <= pos < right: | |
new_lights[pos + 1] ^= 1 | |
# top | |
if row > 0: | |
new_lights[pos - M] ^= 1 | |
# bottom | |
if row < N - 1: | |
new_lights[pos + M] ^= 1 | |
return new_lights | |
def search(cur_pos, cur_lights, N, M, hist=[]): | |
result = [] | |
for pos in range(cur_pos, N * M): | |
new_hist = hist + [pos] | |
new_lights = switch(cur_lights, pos, N, M) | |
if sum(new_lights) == 0: | |
result.append(new_hist) | |
else: | |
result += search(pos + 1, new_lights, N, M, new_hist) | |
return result | |
N, M = map(int, input().split(' ')) | |
lights_string = '' | |
for _ in range(N): | |
lights_string += input().rstrip() | |
lights = [] | |
for lgt in lights_string: | |
if lgt == '.': | |
lights.append(0) | |
else: | |
lights.append(1) | |
for hist in search(0, lights, N, M): | |
print(hist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment