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:

0 D铆as
10 Hrs
53 Min
7 Seg

Playground: Number of Islands

16/52

Contributions 4

Questions 0

Sort by:

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

```python def numeroDeIslas(mapa): # Tu c贸digo aqu铆 馃憞 numero_islas = 0 for i, row in enumerate(mapa): for j, item in enumerate(row): if item == "1": numero_islas += dfs(i, j, mapa) return numero_islas def dfs(i, j, mapa) : mapa[i][j] = "2" if j+1 < len(mapa[i]) and mapa[i][j+1] == "1": dfs(i, j+1, mapa) if i+1 < len(mapa) and mapa[i+1][j] == "1": dfs(i+1, j, mapa) if j-1 >= 0 and mapa[i][j-1] == "1": dfs(i, j-1, mapa) if i-1 >= 0 and mapa[i-1][j] == "1": dfs(i-1, j, mapa) return 1 ```
Comparto mi soluci贸n en Python: ``` from typing import List class Solution: def numIslands(self, grid: List\[List\[str]]) -> int: islands\_counter = 0 def DFS(i, j): if grid\[i]\[j] == '0': return grid\[i]\[j] = '0' # go up if i -1 >= 0: DFS(i-1, j) # go down if i + 1 < len(grid): DFS(i+1, j) # go left if j - 1 >= 0: DFS(i, j-1) # go right if j + 1 < len(grid\[i]): DFS(i, j+1) if grid is None or not grid: return 0 for i in range(len(grid)): for j in range(len(grid\[i])): if grid\[i]\[j] == '1': DFS(i,j) islands\_counter += 1 return islands\_counter if \_\_name\_\_ == "\_\_main\_\_": solution = Solution() # Instantiate the Solution class \# Example 1 grid1 = \[ \["1","1","1","1","0"], \["1","1","0","1","0"], \["1","1","0","0","0"], \["0","0","0","0","0"] ] print("Number of islands in grid1: ", solution.numIslands(grid1)) \# Example 2 grid2 = \[ \["1","1","0","0","0"], \["1","1","0","0","0"], \["0","0","1","0","0"], \["0","0","0","1","1"] ] print("Number of islands in grid2: ", solution.numIslands(grid2)) \# Example 3 - Edge case (empty grid) grid3 = \[] print("Number of islands in grid3: ", solution.numIslands(grid3)) \# Example 4 - Edge case (all water) grid4 = \[ \["0","0","0","0"], \["0","0","0","0"] ] print("Number of islands in grid4: ", solution.numIslands(grid4)) ```
```js def map_island(island_pos, mapa, searched): stack = [island_pos] while stack: current_pos = stack.pop() if(not current_pos in searched): searched.append(current_pos) adjeisent = [ [current_pos[0] + 1, current_pos[1]], [current_pos[0] - 1, current_pos[1]], [current_pos[0], current_pos[1] + 1], [current_pos[0], current_pos[1] - 1] ] for position in adjeisent: if(not position in searched and position[0] < len(mapa) and position[0] > -1 and position[1] < len(mapa[0]) and position[1] > -1 and mapa[position[0]][position[1]] == "1" ): stack.append(position) def numeroDeIslas(mapa): # Tu c贸digo aqu铆 馃憞 islands = 0 stack = [[0,0]] searched = [] for i in range(0, len(mapa)): for j in range(0, len(mapa[0])): current_pos = [i, j] if(mapa[current_pos[0]][current_pos[1]] == "1" and not current_pos in searched ): islands += 1 map_island(current_pos, mapa, searched) searched.append(current_pos) return islands ```Mi soluci贸n, sin usar recursi贸n :)

Mi soluci贸n:


def numeroDeIslas(mapa):
   # Tu c贸digo aqu铆 馃憞
   filas = len(mapa)
   if filas > 0:
        columnas = len(mapa[0]) 

   """ Primera parte encontrar las cooedernas de todos los "1" en el mapa """
   coordenadas=[(1,1)]
   coordenadas.pop()
   index_x = 0
   for x in mapa:
      index_y =0
      for y in x:
         if y == "1":
            coordenadas.append((index_x, index_y))
         index_y += 1
      index_x += 1

   """ segunda parte encontrar la cantidad de islas en el mapa"""

   count_islands = 0



   for island in coordenadas:
      current_island = [island]
      x = island[0]
      y = island[1]
      if mapa[x][y] == "0":
         continue
      else:
         print(x,y)
         count_islands += 1
      for  here   in current_island:
         x  = here[0]
         y = here[1] 
         mapa[x][y] = "0"

         """up"""
         if  y+1 < columnas:
            if mapa[x][y+1] == "1":
               current_island.append((x,y+1))


         """rigth"""
         if x+1 < filas:
            if mapa[x+1][y] == "1":
               current_island.append((x+1,y))


         """down"""
         if y-1 >= 0:
            if mapa[x][y-1] == "1":
               current_island.append((x,y-1))


         """left"""
         if x-1 >= 0:
            if mapa[x-1][y] == "1":
               current_island.append((x-1,y))


   return count_islands
undefined