Introducci贸n

1

驴Qu茅 es un grafo?

2

驴Qu茅 es un 谩rbol?

3

驴Qu茅 es recursi贸n?

4

Aplicaciones reales de grafos y 谩rboles

5

Formas de representar un grafo

DFS

6

An谩lisis de DFS: algoritmo de b煤squeda en profundidad

7

Programando DFS de forma recursiva

8

Otras formas de programar DFS

9

Recorridos y profundidad de un 脕rbol

10

Sum Root to Leaf Numbers: an谩lisis del problema

11

Soluci贸n de Sum Root to Leaf Numbers

12

Playground: Sum Root to Leaf Numbers

13

Programando Sum Root to Leaf Numbers en Golang

14

Number of Islands: an谩lisis del problema

15

Soluci贸n de Number of Islands

16

Playground: Number of Islands

17

Programando Number of Islands en Python

18

Ejercicios recomendados de DFS

19

Ejercicios resueltos de DFS

BFS

20

An谩lisis de BFS: algoritmo de b煤squeda en anchura

21

Programando BFS con Python

22

Minimum Knights Moves (movimientos de caballo en ajedrez): an谩lisis del problema

23

Soluci贸n de Minimum Knights Moves

24

Playground: Minimum Knights Moves

25

Programando Minimum Knights Moves con Python

26

Rotting Oranges: an谩lisis del problema

27

Soluci贸n de Rotting Oranges

28

Playground: Rotting Oranges

29

Rotting Oranges con Java

30

Shortest Bridge Between Islands: an谩lisis del problema

31

Soluci贸n de Shortest Bridge Between Islands

32

Playground: Shortest Bridge Between Islands

33

Programando Shortest Bridge Between Islands con Python

34

Ejercicios recomendados de BFS

35

Ejercicios resueltos de BFS

Backtrack

36

Algoritmo de Backtrack

37

Letter Combinations of a Phone Number: an谩lisis del problema

38

Soluci贸n de Letter Combinations of a Phone Number

39

Programando Letter Combinations of a Phone Number con C++

40

Playground: Letter Combinations of a Phone Number

41

Restore IP Addresses: an谩lisis del problema

42

Programando Restore IP Addresses con C++

43

Playground: Restore IP Addresses

44

Word Search: an谩lisis del problema

45

Soluci贸n de Word Search

46

Playgrund: Word Search

47

Programando Word Search JavaScript

48

Reto: N Queens Puzzle

49

Ejercicios recomendados de Backtrack

50

Ejercicios resueltos de Backtrack

Pr贸ximos pasos

51

驴Qu茅 otros algoritmos y tipos de grafos puedes aprender?

52

驴Quieres m谩s cursos avanzados de algoritmos?

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

1 D铆as
4 Hrs
12 Min
16 Seg

Programando Minimum Knights Moves con Python

25/52
Resources

How to implement an effective solution for complex problems with BFS and Python?

There are no limits for those who decide to tackle complex programming problems. Every line of code brings us closer to the absolute mastery of a language or a logical structure. Today I will guide you through a complex problem solved in an efficient way, using BFS (Breadth-First Search) in Python. Dive in and explore how to become a master of logic with data structures!

What is the basic structure of the problem and its solution?

First, let's understand what we are going to solve. The essence of our problem is to determine the correct way to implement BFS using a data structure with Python. The main function receives four parameters: originX, originY, targetX and targetY, which represent the start and target coordinates of our knight on a chessboard.

  1. Knight's directions: We create a list called directions with eight possible pairs of coordinates, each pair representing a possible jump of the knight.
  2. List of visited squares: We use a hash set to keep a fast and efficient record of the visited squares, avoiding many iterations.
  3. Count of jumps: We define a variable to store the jumps performed, increasing its value when reaching each new level within the BFS.
  4. BFS queue: The queue is essentially a Python list where we initialize our origin position(originX, originY).
# List of possible knight addressesaddresses = [(-1, 2), (1, 2), (-1,-2), (1,-2), (-2, 1), (-2,-1), (2, 1), (2, 1), (2,-1)]
 # Set for already visited positionsvisited = set()
 # Hop counthop_count hop_count = 0
 # Queue with start coordinatesqueue = [(originX, originY)]

How to manage the elements within the BFS?

When we use the BFS, one of the most important aspects is to correctly manage the levels and boxes we check, always trying to reach the target position.

  • Exit condition: We check if the current position matches the target position.
  • Check boxes: For each possible box from the current position, we check if it has already been visited. If not, we add it to the queue.
  • Level increment: Once all the cells of a level have been explored, we increment the number_of_jumps.
while queue: # We check the current cell actual_x, actual_y = queue.pop(0)    
 # If we reach the target position if actual_x == targetX and actual_y == targetY: return jump_amount
 # We check the possible directions for dx, dy in directions: new_x, new_y = actual_x + dx, actual_y + dy if (new_x, new_y) not in visited:visited.add((new_x, new_y)) queue.append((new_x, new_y))    
 # Increase number of jumps after passing a level number_of_jumps += 1

What recommendations to follow to optimize troubleshooting with BFS?

To master the use of BFS in complex problems:

  1. Understand the problem well: knowing well how BFS works and knowing how to apply it to your specific problem is key.
  2. Extend solutions: Try changing the basic problem. For example, try solving it in another programming language or modify the initial conditions for better understanding.
  3. Iterate and test: Before running formal tests, perform desktop tests. This allows you to detect possible bugs before running the code.
  4. Collaborate and share: Share your experiences and solutions with others to get valuable feedback to improve your skills.

With these guidelines, you will optimize your solutions, and improve execution times. Don't forget that you can always improve and adapt to any challenge - go ahead and fortify your programming skills!

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Esta clase me hizo acordar, o le relacione con matem谩ticas discretas, la forma de agrupar las filas, seria bueno que los profesores tanto de la universidad o los de platzi expliquen como funciona cada cosa, porque esto es matem谩tica y comprender sin tener alguna base matem谩tica se me hace mas confuso

Soluci贸n en Python validando el tama帽o del tablero

Siendo A y B el tama帽o
C y D el origen
E y F el destino

class Solution:
   def knight(self, A, B, C, D, E, F):
       direcciones = [[1,2],[1,-2],[-1,2],[-1,-2],[2,1],[2,-1],[-2, 1], [-2, -2]]
       visitados = set()
       cantidadDeSaltos = 0
       cola = [[C,D]]
       while cola: 
           for _ in range(len(cola)):
               actualX, actualY= cola.pop(0)
               if(E == actualX and F == actualY):
                   return cantidadDeSaltos
               if(actualX, actualY) in visitados: continue
               visitados.add((actualX, actualY))
               for direccion in direcciones:
                   if(actualX + direccion[0] < 0 or actualX + direccion[0] > A or actualY + direccion[1] < 0 or actualY + direccion[1] > B): continue
                   cola.append([actualX + direccion[0], actualY + direccion[1]])  
           cantidadDeSaltos += 1
       return cantidadDeSaltos