def manhattan_distance(puzz, end):
"""
Manhettan distance heuristic
"""
m = eval(puzz)
a = eval(end)
lst = []
result = 0
for x in range(4):
for y in range(4):
if a[x][y] == 0:
continue
lst.append([[x, y], a[x][y]])
for i in range(4):
for j in range(4):
if m[i][j] == 0:
continue
for s in lst:
if s[1] == m[i][j]:
x = s[0][0]
y = s[0][1]
result += abs(i - x) + abs(j - y)
return result
for(x=0;x<size-1;x++)
for(y=x+1;y<size;++y)
if (array1[x]>array2[y])
{
temp=array1[x];
array1[x]=array1[y];
array1[y]=temp;
temp=array2[x];
array2[x]=array2[y];
array2[y]=temp;
}
bolts = (4, 3, 2, 1, 5)
gaiki = (1, 2, 3, 4, 5)
sp = [] # тут будет список списков
for bolt in bolts:
for gaika in gaiki:
if gaika == bolt:
sp.append([gaika, bolt]) # добавляем пары в список sp
print(sp)
n = 1
while n < len(sp): # пузырьковая сортировка.
for i in range(len(sp)-n):
if sp[i][0] > sp[i+1][1]: # сравниваем болт и гайку.
sp[i],sp[i+1] = sp[i+1],sp[i]
n += 1
print(sp)