Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Nonogram Row by Sim0000
def nonogram_row(row_string, clue_numbers):
def build(log):
"convert log form to row form"
row = ['X'] * len(row_string)
for p, n in zip(log, clue_numbers):
row[p:p+n] = ['O'] * n
return ''.join(row)
def check(row):
"check found row match row_string or not"
for i, j in zip(row, row_string):
if j == '?': continue
if i != j: return False
return True
def append(row):
"add new found row to answer"
for i in range(n):
answer[i].add(row[i])
def search(last_pos, log, clue):
"generate all arrangement and check it by recursive search"
if not clue:
row = build(log)
if check(row): append(row)
return
for p in range(last_pos, n - clue[0] + 1):
search(p + clue[0] + 1, log + [p], clue[1:])
"start here"
n = len(row_string)
answer = [set() for _ in range(n)]
"search all arrangement and accumulate answer"
search(0, [], clue_numbers)
if any(i == set() for i in answer): return
return ''.join('?' if len(answer[i]) > 1 else answer[i].pop() for i in range(n))
if __name__ == '__main__':
assert nonogram_row('??????????', [8]) == '??OOOOOO??', 'Simple boxes_1'
assert nonogram_row('??????????', [4, 3]) == '??OO???O??', 'Simple boxes_2'
assert nonogram_row('???O????O?', [3, 1]) == 'X??O??XXOX', 'Simple spaces'
assert nonogram_row('????X?X???', [3, 2]) == '?OO?XXX?O?', 'Forcing'
assert nonogram_row('O?X?O?????', [1, 3]) == 'OXX?OO?XXX', 'Glue'
assert nonogram_row('??OO?OO???O?O??', [5, 2, 2]) == 'XXOOOOOXXOOXOOX', 'Joining and splitting'
assert nonogram_row('????OO????', [4]) == 'XX??OO??XX', 'Mercury'
print("Check done.")
Dec. 28, 2017
Comments: