'' ' 0: gauche 1: droite 2: haut 3: bas ' '' chemins = [] def chemin_dans_matrix (matrice, s): ligne, col = len (matrice), len (matrice [0]) chemins globaux chemins = [] si len (s) == 0: retourne True def go_next (matrice, i, j, s, src): si len (s) == 0: retourne True si i> = ligne ou j> = col ou matrice [i] [j]! = s [0]: renvoie False paths.append ((i, j)) si src! = 0 et go_next (matrice, i, j-1, s [1:], 1): retourne True si src! = 1 et go_next (matrice, i, j + 1, s [1:], 0): retourne True si src! = 2 et go_next (matrice, i-1, j, s [1:], 3): retourne Vrai si src! = 3 et go_next (matrice, i + 1, j, s [1:], 2 ): return True paths.pop () return False for i in range (row): for j in range (col): if matrix [i] [j] == s [0]: paths.append ((i, j )) si go_next (matrice, i, j-1, s [1:], 1): #go from right return True si go_next (matrice, i, j + 1, s [1:], 0): #go de gauche retourne True si go_next (matrice, i-1, j, s [1:], 3): #go de bas retour True if go_next (matrix, i + 1, j, s [1:], 2): #go from up return True paths.pop () return False if __name__ == "__main__": matrix = [['a', ' b ',' t ',' g '], [' c ',' f ',' c ',' s '], [' j ',' d ',' e ',' h '], ] imprimer (path_in_matrix (matrice, 'bfce')) print (chemins) print (path_in_matrix (matrice, 'abfb')) print (chemins) print (path_in_matrix (matrice, 'tceh')) print (chemins)
Offre de doigt d'épée (12) ------ chemin dans la matrice
Je suppose que tu aimes
Origine blog.csdn.net/happyAnger6/article/details/104762523
conseillé
Classement