No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

# Programando Number of Islands en Python

17/52
Recursos

Aportes 4

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

En Java

``````class Solution {
static int islandNumbers;
public int numIslands(char[][] grid) {
islandNumbers = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length ; j++){
if(grid[i][j] == '1'){
dfs(i,j, grid);
islandNumbers++;
}
}
}
return islandNumbers;
}

public void dfs(int i, int j, char[][] grid){
if(i>=0 && i<grid.length && j>=0 && j<grid[0].length && grid[i][j] == '1'){
grid[i][j] = '2';
dfs(i, j+1, grid);
dfs(i, j-1, grid);
dfs(i+1, j, grid);
dfs(i-1, j, grid);

}
}
}
``````

Esta es mi solución en Kotlin:

``````fun main() {
val map = listOf(
mutableListOf(1, 1, 0, 0, 0, 0, 1, 0),
mutableListOf(1, 0, 0, 1, 1, 0, 0, 0),
mutableListOf(0, 0, 0, 1, 1, 0, 0, 1),
mutableListOf(0, 0, 0, 0, 0, 1, 1, 1),
)

val count = countIslands(map);

println("The number of Island in the map is: \$count")
}

fun countIslands(map: List<MutableList<Int>>): Int {
var count = 0
for (i in map.indices) {
for (j in map[i].indices) {
if (map[i][j] == 1) {
inspectIsland(map, i, j)
count++
}
}
}
return count
}
``````
La solución pasada a C# `List<List<int>> inputData = new()` `{` ` new List<int>() { 1, 1, 1, 0, 0 },` ` new List<int>() { 1, 1, 0, 0, 0 },` ` new List<int>() { 0, 0, 0, 1, 1 },` ` new List<int>() { 1, 1, 0, 0, 0 },` ` new List<int>() { 1, 1, 0, 1, 1 }` `};` `void dfsInslands(int i, int j)` `{` ` if (0 <= i && i < inputData.Count && 0 <= j && j < inputData.Count && inputData[i][j] == 1)` ` {` ` inputData[i][j] = 2;` ` dfsInslands(i + 1, j);` ` dfsInslands(i - 1, j);` ` dfsInslands(i, j - 1);` ` dfsInslands(i, j + 1);` ` }` `}` `int CountIslands(List<List<int>> islands)` `{` ` int count = 0;` ` for (int i = 0; i < islands.Count; i++)` ` for (int j = 0; j < islands.Count; j++)` ` if (islands[i][j] == 1)` ` {` ` dfsInslands(i, j);` ` count++;` ` }` ` return count;` `}` `Console.WriteLine("TOTAL OF ISLANDS {0}",CountIslands(inputData));`

Jeje si leí el cuento de Hansel y Gretel.