1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| n = 4
friends = [[False for _ in range(n)] for _ in range(n)]
friends[0][1] = friends[1][0] = True friends[1][2] = friends[2][1] = True friends[2][3] = friends[3][2] = True friends[3][0] = friends[0][3] = True friends[0][2] = friends[2][0] = True friends[1][3] = friends[3][1] = True
has_pair = [False for _ in range(n)]
def solve(has_pair): first = None for i in range(n): if has_pair[i] == False: first = i break
if first == None: return 1
ret = 0 for student in range(first+1, n): if has_pair[student] == False and friends[first][student] == True: has_pair[student] = has_pair[first] = True ret += solve(has_pair) has_pair[student] = has_pair[first] = False
return ret
print(solve(has_pair))
|