As a work needs to develop an algorithm that calculates the amount of possible answers to a puzzle, the game consists of an arrangement of n black / white pieces, when a piece is removed the adjacent color change, only pieces can be removed and the goal is to remove all the pieces. After a few hours looking at the code I can not see the error, the algorithm is only following 1 path and in the recursions return apparently it does not continue the iteration for
. The example response contained in the code should be 11, it is returning 1.
finished = 0
def jogovira(lista, n):
global finished
if (solucionado(lista, n)):
finished += 1
print("solucao encontrada!")
return
else:
for i in range(n):
if lista[i] == 'p':
novalista = tira(lista, i, n)
jogovira(novalista, n)
return
def solucionado(l, n):
for f in range(n):
if (l[f] != 'x'):
return False
return True
def tira(l, j, n):
m = l
m[j] = 'x'
if (j<n-1):
m[j+1] = troca(l, j+1)
if (j>0):
m[j-1] = troca(l, j-1)
return m
def troca(v, k):
if (v[k] == 'x'):
return 'x'
if (v[k] == 'p'):
return 'b'
if (v[k] == 'b'):
return 'p'
jogovira(['b', 'p', 'b', 'p', 'p'], 5)
print(finished)