Skip to content

Instantly share code, notes, and snippets.

@mvgolom
Last active March 12, 2020 20:17
Show Gist options
  • Save mvgolom/a6f50eceaed8026070a3b1b58cd85d86 to your computer and use it in GitHub Desktop.
Save mvgolom/a6f50eceaed8026070a3b1b58cd85d86 to your computer and use it in GitHub Desktop.
50 50
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0
0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0
0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0
0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 0
0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0
0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0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 0 0 0 1 0 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
import argparse
import tocaLib
from pprint import pprint
from drawnow import drawnow
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument("--filepath", type=str,default="texto.txt",help="Caminho para a configuração")
parser.add_argument("--showpath", action='store_true',help="Mostra passo a passo o caminho percorrido de forma grafica")
parser.add_argument("--slow",action='store_true',help="Diminui a velocidade da animação")
parser.add_argument("--showstack",action='store_true',help="Mostar a stack de caminhos")
args = parser.parse_args()
emilia = tocaLib.Emilia(slow=args.slow,filePath=args.filepath,stack=args.showstack)
emilia.loadIn()
fig = []
if args.showpath:
plt.ion() # enable interactivity
fig = plt.figure() # make a figure
emilia.showpath = True
if(emilia.showpath):
drawnow(emilia.make_fig)
#pprint(emilia.matriz2)
doing = True
if(emilia.initPos == ()):
print("A sala inicial não foi encontrada")
sys.exit()
elif(emilia.finalPos == ()):
print("A sala final não foi encontrada")
sys.exit()
emilia.currentPos = emilia.initPos
while doing != False:
#print(len(emilia.previousPath))
neighbors = emilia.getneighbors(emilia.currentPos)
if(emilia.posIsEqual(emilia.currentPos, emilia.finalPos)):
print(len(emilia.previousPath)+1)
break
if(len(emilia.previousPath) == 0):
path = emilia.createPath(neighbors)
emilia.previousPath.append(emilia.currentPos)
if(len(path) > 1):
emilia.crossing.append(str(emilia.currentPos))
pos = emilia.currentPos
emilia.move(path.pop())
emilia.multiplePath.append((pos, path))
elif(len(path) == 1):
emilia.move(path.pop())
else:
print("Não foi encontrado nenhum caminho")
sys.exit()
if(emilia.showpath):
drawnow(emilia.make_fig)
#pprint(emilia.matriz2)
else:
previous = emilia.previousPath.pop()
emilia.previousPath.append(previous)
path = emilia.createPath(neighbors, previous)
emilia.previousPath.append(emilia.currentPos)
if(emilia.matriz[emilia.currentPos[0]][emilia.currentPos[1]] == 3):
break
if(len(path) != 0):
if(emilia.matriz[emilia.currentPos[0]][emilia.currentPos[1]] == 3):
break
if(len(path) > 1):
emilia.crossing.append(str(emilia.currentPos))
pos = emilia.currentPos
emilia.move(path.pop())
emilia.multiplePath.append((pos, path))
else:
emilia.move(path.pop())
if(emilia.showpath):
drawnow(emilia.make_fig)
#pprint(emilia.matriz2)
else:
if(emilia.matriz[emilia.currentPos[0]][emilia.currentPos[1]] == 3):
break
if(len(emilia.multiplePath) != 0):
pos, path = emilia.multiplePath[-1]
emilia.toBack = pos
emilia.rollBack()
else:
print("Não foi encontrado nenhum caminho")
break
4 5
0 1 1 1 0
0 2 0 1 1
0 0 0 0 1
3 1 1 1 1
4 5
0 0 0 1 0
0 0 0 1 1
0 0 0 0 2
0 3 1 1 1
4 5
0 1 2 1 0
0 1 0 1 1
0 0 0 0 1
3 1 1 1 1
4 5
0 1 1 1 0
0 1 0 2 1
0 0 0 0 1
3 0 1 1 1
4 5
0 1 2 1 0
0 1 0 1 1
0 0 0 0 1
1 1 1 1 1
4 5
0 1 1 1 0
0 1 0 1 1
0 0 0 0 1
3 1 1 1 1
import os
import sys
import matplotlib.pyplot as plt
plt.style.use("dark_background")
class Emilia:
def __init__(self,filePath="texto.txt",slow=False,stack=False):
self.matriz = []
self.matriz2 = []
self.initPos = ()
self.finalPos = ()
self.currentPos = (0, 0)
self.linhas = 0
self.colunas = 0
self.possibleMoves = ["cima", "direita", "baixo", "esquerda"]
self.previousPath = []
self.multiplePath = []
self.crossing = []
self.toBack = ()
self.showpath = False
self.figPath = []
self.filePath = filePath
self.slow = slow
self.stack = stack
def loadIn(self):
raw_txt = []
with open(self.filePath, 'r') as archive:
for line in archive:
raw_txt.append(line)
aux = raw_txt[0].replace("\n", "")
self.linhas, self.colunas = raw_txt[0].split(" ")
self.linhas = int(self.linhas.strip())
self.colunas = int(self.colunas.strip())
for x in range(1, len(raw_txt)):
aux = raw_txt[x].replace("\n", "")
linha = aux.split(" ")
linha = [int(i) for i in linha]
self.matriz.append(linha)
self.matriz2.append(linha)
for i in range(0, self.linhas):
for j in range(0, self.colunas):
if(self.matriz[i][j] == 2):
self.initPos = (i, j)
if(self.matriz[i][j] == 3):
self.finalPos = (i, j)
self.colunas = self.colunas-1
self.linhas = self.linhas-1
def getneighbors(self, pos):
i, j = pos
if(j > 0):
esquerda = self.matriz[i][j-1]
else:
esquerda = 0
if(j < self.colunas):
direita = self.matriz[i][j+1]
else:
direita = 0
if(i > 0):
cima = self.matriz[i-1][j]
else:
cima = 0
if(i < self.linhas):
baixo = self.matriz[i+1][j]
else:
baixo = 0
neighbors = {
"cima": [(i-1, j), cima],
"direita": [(i, j+1), direita],
"baixo": [(i+1, j), baixo],
"esquerda": [(i, j-1), esquerda]
}
return neighbors
def move(self, direction):
i, j = self.currentPos
if(direction[0] == "esquerda"):
self.currentPos = (i, j-1)
self.matriz2[i][j-1] = 4
elif(direction[0] == "direita"):
self.currentPos = (i, j+1)
self.matriz2[i][j+1] = 4
elif(direction[0] == "cima"):
self.currentPos = (i-1, j)
self.matriz2[i-1][j] = 4
elif(direction[0] == "baixo"):
self.currentPos = (i+1, j)
self.matriz2[i+1][j] = 4
def posIsEqual(self, pos1, pos2):
i1, j1 = pos1
i2, j2 = pos2
if((i1 == i2) and (j1 == j2)):
return True
else:
return False
def createPath(self, direction, previous=None):
path = []
if(previous == None):
for direct in self.possibleMoves:
aux = direction[direct]
if((aux[1] == 1) or (aux[1] == 3)):
path.append((direct, aux[0], aux[1]))
elif(str(self.currentPos) in self.crossing):
pathaux = self.multiplePath[-1]
path = self.multiplePath.pop()
path = path[1]
else:
for direct in self.possibleMoves:
aux = direction[direct]
if((aux[1] == 1) or (aux[1] == 3)):
if(self.posIsEqual(previous, aux[0]) == False):
path.append((direct, aux[0], aux[1]))
return path
def rollBack(self):
for x in range(len(self.previousPath)):
pos = self.previousPath.pop()
self.currentPos = pos
if(self.stack):
self.matriz2[pos[0]][pos[1]] = 10
if(self.posIsEqual(self.toBack, pos) == True):
break
def make_fig(self):
plt.matshow(self.matriz2,'Path')
if(self.slow):
plt.pause(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment