0
As an accurate work 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 ones change color, only black pieces can be removed and the goal is to remove all the pieces. After a few hours looking at the code I can’t see the error, the algorithm is only following 1 path and around recursions apparently it doesn’t continue iteration for
. The sample response contained in the code should be 11, 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)
I don’t quite understand the algorithm, there’s a link somewhere with the description?
– felipsmartins
this link that explains the problem: http://olimpiada.ic.unicamp.br/pratique/programacao/nivel2/2011f2p2_vira the game function is the main function that tried to make all possible paths recursively, the solved function tests whether arrived in a response, the strip function removes a part and calls the swap function which inverts the adjacent parts. .
– Gabriel Almeida
So I assume you’ve already solved your problem. How about posting your answer and marking it as the right answer? That’s possible here in the OS.
– felipsmartins