import itertools as it
t = '''2659862637316867 ;2
1841236454324589 ;3
3041631117224635 ;3
4895722652190306 ;1
1748270476758276 ;3
5251583379644322 ;2
2326509471271448 ;2
2321386104303845 ;0
6442889055042768 ;2
6913859173121360 ;1
6375711915077050 ;1
8690095851526254 ;3
2615250744386899 ;2
8157356344118483 ;1
7890971548908067 ;3
4513559094146117 ;2
3174248439465858 ;1
4296849643607543 ;3
9742855507068353 ;3
5855462940810587 ;3
3847439647293047 ;1
5616185650518293 ;2'''
s = []
for i in t.split('\n'):
f = i.strip()
m = []
for j in f.split(';'):
j = j.strip()
m.append(j)
s.append(m)
s1 =[]
for each in s:
m = []
for i in each[0]:
m.append(int(i))
s1.append((m,int(each[1])))
t = s1[7]
s1.remove(t)
def shai_1():
weizhi1 = []
for i in s1:
m = []
for j in range(16):
if i[0][j] != t[0][j]:
m.append(j)
weizhi1.append(m)
return weizhi1
weizhi = shai_1()
def sheng():
S = []
for t in it.combinations(weizhi[0],s1[0][1]):
s_t = ['x']*16
for x1 in range(s1[0][1]):
s_t[t[x1]] = s1[0][0][t[x1]]
s_x = s_t[:]
S.append((s_x,t))
return S
def shai_2(S, t, c, weizhi):
if len(t)==16:
V = 1
for a in range(21):
count = 0
for each in range(16):
if S[each] == s1[a][0][each]:
count +=1
if count != s1[a][1]:
V =0
break
if V == 1:
print(S)
return 1
for i in range(c,21):
s_t = S[:]
flag = 1
for j in weizhi[i]:
if s_t[j] == s1[i][0][j]:
flag = 0
break
if flag == 1:
t1 = [k for k in weizhi[i] if k not in t]
for x in it.combinations(t1,s1[i][1]):
t2 = t
s_y = s_t[:]
for y in range(s1[i][1]):
s_y[x[y]] = s1[i][0][x[y]]
s_z = s_y[:]
t2 += x
c = i
shai_2(s_z, t2, c, weizhi)
def main():
M = sheng()
for i in M:
S,t = i[0],i[1]
shai_2(S,t,1,weizhi)
main()
|