בניתי משהו אבל אני לא בטוח למה עבור מה שנתת הוא מחזיר שאין פתרון. אולי באג אצלי
import itertools
a = [[3, 3, 3, 2], [2, 1, 3, 4], [1, 3, 4, 3], [3, 3, 2, 3], [3, 2, 3, 3], [2, 3, 3, 3],
[1, 3, 4, 2], [3, 4, 2, 1], [4, 2, 1, 3], [3, 4, 3, 1], [4, 3, 1, 3], [3, 1, 3, 4]]
b = [[3, 3, 1, 4], [1, 1, 3, 2], [1, 4, 2, 3], [3, 1, 4, 3], [1, 4, 3, 3], [4, 3, 3, 1],
[1, 3, 2, 1], [3, 2, 1, 1], [2, 1, 1, 3], [4, 2, 3, 1], [2, 3, 1, 4], [3, 1, 4, 2]]
c = [[1, 3, 4, 4], [3, 2, 4, 2], [1, 2, 4, 2], [3, 4, 4, 1], [4, 4, 1, 3], [4, 1, 3, 4],
[2, 4, 2, 3], [4, 2, 3, 2], [2, 3, 2, 4], [2, 4, 2, 1], [4, 2, 1, 2], [2, 1, 2, 4]]
d = [[3, 2, 2, 1], [1, 2, 4, 1], [1, 3, 4, 2], [2, 2, 1, 3], [2, 1, 3, 2], [1, 3, 2, 2],
[2, 4, 1, 1], [4, 1, 1, 2], [1, 1, 2, 4], [3, 4, 2, 1], [4, 2, 1, 3], [2, 1, 3, 4]]
e = [a, b, c, d]
def is_good_length(iterables):
for i in range(len(iterables)):
if len(iterables[i]) != len(iterables[i - 1]):
return False
if isinstance(iterables[i][0], list) and not is_good_length(iterables[i]):
return False
return True
def get_solution_by_indexes(iterables, possible_indexes):
items = []
for iterable, index in zip(iterables, possible_indexes):
items.append(iterable[index])
return items
def check_solution(iterables):
print(iterables)
for i in zip(*iterables):
if len(set(i)) != len(i):
return False
return True
def solve(iterables):
indexes_generator = itertools.product(*[list(range(len(iterables[0])))] * len(iterables))
for possible_indexes in indexes_generator:
possible_solution = get_solution_by_indexes(iterables, possible_indexes)
#print(possible_solution)
if check_solution(possible_solution):
return possible_solution
assert is_good_length(e)
solve(e)
עבור הקלט הבא שיצרתי כן יש פתרון, והקוד מוצא אותו:
a = [[1, 2, 3, 4], [2, 1, 3, 4], [1, 3, 4, 3], [3, 3, 2, 3], [3, 2, 3, 3], [2, 3, 3, 3],
[1, 3, 4, 2], [3, 4, 2, 1], [4, 2, 1, 3], [3, 4, 3, 1], [4, 3, 1, 3], [3, 1, 3, 4]]
b = [[3, 3, 1, 4], [1, 1, 3, 2], [1, 4, 2, 3], [3, 1, 4, 3], [1, 4, 3, 3], [4, 3, 3, 1],
[1, 3, 2, 1], [2, 3, 4, 1], [2, 1, 1, 3], [4, 2, 3, 1], [2, 3, 1, 4], [3, 1, 4, 2]]
c = [[1, 3, 4, 4], [3, 2, 4, 2], [1, 2, 4, 2], [3, 4, 4, 1], [4, 4, 1, 3], [4, 1, 3, 4],
[2, 4, 2, 3], [4, 2, 3, 2], [3, 4, 1, 2], [2, 4, 2, 1], [4, 2, 1, 2], [2, 1, 2, 4]]
d = [[3, 2, 2, 1], [1, 2, 4, 1], [1, 3, 4, 2], [2, 2, 1, 3], [2, 1, 3, 2], [1, 3, 2, 2],
[2, 4, 1, 1], [4, 1, 1, 2], [1, 1, 2, 4], [3, 4, 2, 1], [4, 1, 2, 3], [2, 1, 3, 4]]