Cvičení 13

Řešení problémů

nadoby = list(map(int, input().split()))
x = int(input())

o=[[[0,0,0],'']]
c=[]
end = False

def konec(s):
end = False
for i in range(3):
if s[0][i]==x:
end = True
return end

def stejny(a,b):
return (a[0][0]==b[0][0]) and (a[0][1]==b[0][1]) and (a[0][2]==b[0][2])

def novy(s):
ret = True
for x in o:
if stejny(s,x):
ret = False
break
if ret:
for x in c:
if stejny(s,x):
ret = False
break
return ret

while len(o)>0 and not end:
s = o.pop(0)
c.append(s)
print('Stav',s)
for i in range(3):
s2 = [[s[0][0], s[0][1], s[0][2]],s[1]]
s2[1]+='N'+str(i)+'\n'
if konec(s2):
print(s2[1])
end=True
break
if novy(s2):
o.append(s2)
print("Novy",s2)

for i in range(3):
s2 = [[s[0][0], s[0][1], s[0][2]],s[1]]
s2[0][i]=0
s2[1]+='V'+str(i)+'\n'
if konec(s2):
print(s2[1])
end=True
break
if novy(s2):
o.append(s2)
print("Novy",s2)
for i in range(3):
if not end:
for j in range(3):
if (j!=i):
s2 = [[s[0][0], s[0][1], s[0][2]],s[1]]
s2[0][j]+=s2[0][i]
s2[0][i]=0
else:
s2[1]+=str(i)+'P'+str(j)+'\n'
if konec(s2):
print(s2[1])
end=True
break
if novy(s2):
print("Novy",s2)
o.append(s2)
if not end:
print("Nelze vyresit")
def spravne(a):
ret = True
for i in range(len(a)-1):
roz = len(a)-1-i
if (a[i]-a[-1])==roz or (a[-1]-a[i])==roz:
ret = False
break
return ret

def kralovny(hotovo, neumistene):
print('s:',hotovo, neumistene)
if len(neumistene)==0:
return(hotovo)
else:
vysl = []
for i in range(len(neumistene)):
hotovo2 = [x for x in hotovo]
neumistene2 = [x for x in neumistene]
hotovo2.append(neumistene[i])
if spravne(hotovo2):
neumistene2.pop(i)
vysl = kralovny(hotovo2,neumistene2)
if (len(vysl)>0):
break
return vysl

print(kralovny([], [0,1,2,3,4,5,6,7]))

def spravne(a):
ret = True
for i in range(len(a)-1):
roz = len(a)-1-i
if (a[i]-a[-1])==roz or (a[-1]-a[i])==roz:
ret = False
break
return ret

def kralovny(hotovo, neumistene, res):
print('s:',hotovo, neumistene)
if len(neumistene)==0:
res.append(hotovo)
return []
else:
vysl = []
for i in range(len(neumistene)):
hotovo2 = [x for x in hotovo]
neumistene2 = [x for x in neumistene]
hotovo2.append(neumistene[i])
if spravne(hotovo2):
neumistene2.pop(i)
vysl = kralovny(hotovo2,neumistene2, res)
if (len(vysl)>0):
break
return vysl
a=[]
kralovny([], [0,1,2,3,4,5,6,7], a)
print(a)