Skip to content

Instantly share code, notes, and snippets.

@EdibertoLima
Last active April 15, 2019 05:30
Show Gist options
  • Save EdibertoLima/ba54700e3b411a3acffbd8b4ebb47b2d to your computer and use it in GitHub Desktop.
Save EdibertoLima/ba54700e3b411a3acffbd8b4ebb47b2d to your computer and use it in GitHub Desktop.
canibais_missionarios
#coding: utf-8
inicio = [["m","m","m","c","c","c"],[0]]
estado = [[[["m","m","m","c","c","c"],[0]], [0,0]]]
iteracoes = [["m",0], ["c",0], ["m","c"],["m","m"],["c","c"]]
continuar = True
while continuar:
#clonando o estado
temp = [[estado[0][0][0][:],estado[0][0][1][:]]]
for i in range(1,len(estado[0])):
temp.append(estado[0][i][:])
#liberando espaço em um estado para o próximo do ítem fila
estado.pop(0)
#testando as iterações possíveis
i=0
while i < len(iteracoes):
#clonando a variavel temp
interno = [temp[0][0][:],temp[0][1][:]]
#verificando se o barco não é igual ao último (isso causaria um loop)
if iteracoes[i] != temp[len(interno)-1]:
#testando se o número de pessoas no barco existia na margem
miss = iteracoes[i].count("m")
cani = iteracoes[i].count("c")
if interno[0].count("m") >= miss and interno[0].count("c") >= cani:
#trocando as pessoas de margem
for m in range(0,miss):
interno[1].append("m")
interno[0].remove("m")
for c in range(0,cani):
interno[1].append("c")
interno[0].remove("c")
#testando se a passagem era adequada às regras do jogo
if interno[0].count("m") == 0 or interno[0].count("m") >= interno[0].count("c"):
if interno[1].count("m") == 0 or interno[1].count("m") >= interno[1].count("c"):
estado.append([[interno[1][:],interno[0][:]]])
for t in range(1,len(temp)):
estado[len(estado)-1].append(temp[t])
estado[len(estado)-1].append(iteracoes[i])
#verificação e impressão de resultado
for l in range(0,2):
if interno[l].count(0)==0 and interno[l] == []:
inicio[1].remove(0)
print("Estado 1","\nmargem 1", inicio[0],"\nmargem 2", inicio[1])
for p in range(2,len(estado[len(estado)-1])):
print("Barco",p-1,estado[len(estado)-1][p])
for b in range(0,2):
if estado[len(estado)-1][p][b] != 0:
if p%2 == 0:
inicio[0].remove(estado[len(estado)-1][p][b])
inicio[1].append(estado[len(estado)-1][p][b])
else:
inicio[1].remove(estado[len(estado)-1][p][b])
inicio[0].append(estado[len(estado)-1][p][b])
print("\nEstado", p,"\nmargem 1", inicio[0],"\nmargem 2", inicio[1])
print("\nEm",len(estado),'tentativas.')
i = len(iteracoes)
continuar = False
i = i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment