-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7de303
commit 9827adc
Showing
9 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
conferences/11-divide-and-conquer/02_SubsecuenciaSumaMaxima/02_SubsecuenciaSumaMaxima.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>_02_SubsecuenciaSumaMaxima</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
160 changes: 160 additions & 0 deletions
160
conferences/11-divide-and-conquer/02_SubsecuenciaSumaMaxima/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
using System.Diagnostics; | ||
int[] CreateRandomArray(int n) | ||
{ | ||
int[] a = new int[n]; | ||
Random r = new Random(); | ||
for (int k = 0; k < n; k++) | ||
{ | ||
//Para que haya negativos en el array los pares los ponemos como positivos | ||
//y los impares como negativos | ||
var j = r.Next(n); | ||
if (j % 2 == 0) | ||
a[k] = j; | ||
else | ||
a[k] = -j; | ||
} | ||
return a; | ||
} | ||
|
||
void Print(int[] a) | ||
{ | ||
for (int k = 0; k < a.Length; k++) | ||
{ | ||
Console.Write("{0,4} ", a[k]); | ||
if ((k + 1) % 10 == 0) Console.WriteLine(); | ||
} | ||
} | ||
|
||
#region SUBSECUENCIA SUMA MAXIMA | ||
//DE ORDEN CÚBICO, n^3 (n longitud del array) | ||
int SubSumaMaxFuerzaBruta(int[] a) | ||
{ | ||
long count = 0; | ||
int maxSuma = 0; | ||
for (int i = 0; i < a.Length; i++) | ||
for (int j = i; j < a.Length; j++) | ||
{ | ||
int suma = 0; | ||
for (int k = i; k <= j; k++) | ||
{ | ||
count++; | ||
suma += a[k]; | ||
} | ||
if (suma > maxSuma) maxSuma = suma; | ||
} | ||
Console.WriteLine("\nSubSumaMax por Fuerza Bruta Cubico n^3 {0} iteraciones", count); | ||
return maxSuma; | ||
} | ||
|
||
//DE ORDEN CUADRÁTICO, n^2 (n longitud del array) | ||
int SubSumaMaxCuadratico(int[] a) | ||
{ | ||
int maxSuma = 0; | ||
long count = 0; | ||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
int suma = 0; | ||
for (int j = i; j < a.Length; j++) | ||
{ | ||
count++; | ||
suma += a[j]; | ||
if (suma > maxSuma) maxSuma = suma; | ||
} | ||
} | ||
Console.WriteLine("\nSubSumaMax por Fuerza Bruta Cuadratico n^2 {0} iteraciones", count); | ||
return maxSuma; | ||
} | ||
|
||
//DIVIDE Y VENCERAS RECURSIVO, n*ln(n) (n LONGITUD DEL ARRAY) | ||
int SubSumaMaxDivideVenceras(int[] a) | ||
{ | ||
long count = 0; | ||
int SubSumaMaxRecursivo(int[] a, int inf, int sup) | ||
{ | ||
count++; | ||
int maxHaciaIzq = 0; int maxHaciaDer = 0; | ||
int medio, mayorSumaIzq, mayorSumaCentro, mayorSumaDer; | ||
if (inf == sup) return (a[inf] > 0 ? a[inf] : 0); | ||
else | ||
{ | ||
medio = (inf + sup) / 2; | ||
mayorSumaIzq = SubSumaMaxRecursivo(a, inf, medio); | ||
mayorSumaDer = SubSumaMaxRecursivo(a, medio + 1, sup); | ||
int sum = 0; | ||
for (int i = medio; i >= inf; i--) | ||
{ | ||
count++; | ||
sum += a[i]; | ||
if (sum > maxHaciaIzq) maxHaciaIzq = sum; | ||
} | ||
sum = 0; | ||
for (int i = medio + 1; i <= sup; i++) | ||
{ | ||
count++; | ||
sum += a[i]; | ||
if (sum > maxHaciaDer) maxHaciaDer = sum; | ||
} | ||
mayorSumaCentro = maxHaciaIzq + maxHaciaDer; | ||
return Math.Max(mayorSumaIzq, Math.Max(mayorSumaCentro, mayorSumaDer)); | ||
} | ||
} | ||
var result = SubSumaMaxRecursivo(a, 0, a.Length - 1); | ||
Console.WriteLine("\nSubSumaMax Div y Vencera n*ln(n) {0} iteraciones", count); | ||
return result; | ||
} | ||
|
||
#region LINEAL | ||
//DE ORDEN LINEAL (EN UN SOLO RECORRIDO DEL ARRAY) | ||
int SubSumaMaxLineal(int[] a) | ||
{ | ||
long count=0; | ||
int maxSuma = 0; | ||
int suma = 0; | ||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
count++; | ||
suma += a[i]; | ||
if (suma > maxSuma) maxSuma = suma; | ||
else if (suma < 0) suma = 0; | ||
} | ||
Console.WriteLine("\nSubSumaMax Lineal n {0} iteraciones", count); | ||
return maxSuma; | ||
} | ||
#endregion | ||
#endregion | ||
|
||
//PRUEBA SUBSECUENCIA SUMA MAXIMA | ||
do | ||
{ | ||
Stopwatch crono = new Stopwatch(); | ||
Console.Write("\nEntre longitud para la secuencia "); | ||
string s = Console.ReadLine(); | ||
if (s.Length == 0) break; | ||
int n = Int32.Parse(s); | ||
var secuencia = CreateRandomArray(n); | ||
int result; | ||
|
||
crono.Restart(); | ||
result = SubSumaMaxFuerzaBruta(secuencia); | ||
crono.Stop(); | ||
Console.WriteLine("SubSumaMax (Fuerza Bruta) Cubico n^3 {0} en {1} ms", result, crono.ElapsedMilliseconds); | ||
|
||
crono.Restart(); | ||
result = SubSumaMaxCuadratico(secuencia); | ||
crono.Stop(); | ||
Console.WriteLine("SubSumaMax Cuadratico n*n {0} en {1} ms", result, crono.ElapsedMilliseconds); | ||
|
||
crono.Restart(); | ||
result = SubSumaMaxDivideVenceras(secuencia); | ||
crono.Stop(); | ||
Console.WriteLine("SubSumaMax Div y Venceras n*ln(n) {0} en {1} ms", result, crono.ElapsedMilliseconds); | ||
|
||
//crono.Restart(); | ||
//result = SubSumaMaxLineal(secuencia); | ||
//crono.Stop(); | ||
//Console.WriteLine("SubSumaMax Lineal n {0} en {1} ms", result, crono.ElapsedMilliseconds); | ||
|
||
} while (true); | ||
|
||
|
Binary file not shown.
31 changes: 31 additions & 0 deletions
31
conferences/12-backtrack/03_Backtracking/03_Backtracking.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.3.32929.385 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "03_UbicarReinas", "03_UbicarReinas.csproj", "{39BC5C46-2F6F-4156-9A2F-4E336B4300A5}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "03_Laberinto", "..\03_Laberinto\03_Laberinto.csproj", "{C2E7CE83-AE61-4C9A-B5A2-6639B8BCE77D}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{39BC5C46-2F6F-4156-9A2F-4E336B4300A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{39BC5C46-2F6F-4156-9A2F-4E336B4300A5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{39BC5C46-2F6F-4156-9A2F-4E336B4300A5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{39BC5C46-2F6F-4156-9A2F-4E336B4300A5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{C2E7CE83-AE61-4C9A-B5A2-6639B8BCE77D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C2E7CE83-AE61-4C9A-B5A2-6639B8BCE77D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C2E7CE83-AE61-4C9A-B5A2-6639B8BCE77D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C2E7CE83-AE61-4C9A-B5A2-6639B8BCE77D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {3529BF63-4697-4FBC-819C-BA0C73EF7A39} | ||
EndGlobalSection | ||
EndGlobal |
11 changes: 11 additions & 0 deletions
11
conferences/12-backtrack/03_Backtracking/03_UbicarReinas.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>_03_Backtracking</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
#region UBICA REINAS EN UN TABLERO DE N*N | ||
|
||
using System.Diagnostics; | ||
|
||
long count; | ||
|
||
void VisualizaTablero(bool[,] tablero) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Black; | ||
for (int i = 0; i < tablero.GetLength(0); i++) | ||
{ | ||
for (int j = 0; j < tablero.GetLength(1); j++) | ||
{ | ||
if (tablero[i, j]) | ||
{ | ||
Console.BackgroundColor = ConsoleColor.Green; | ||
Console.Write("Q "); | ||
} | ||
else | ||
{ | ||
if ((i + j) % 2 == 0) Console.BackgroundColor = ConsoleColor.Red; | ||
else Console.BackgroundColor = ConsoleColor.White; | ||
Console.Write(" "); | ||
} | ||
} | ||
Console.BackgroundColor = ConsoleColor.Black; | ||
Console.WriteLine(); | ||
} | ||
Console.BackgroundColor = ConsoleColor.Black; | ||
Console.ForegroundColor = ConsoleColor.White; | ||
} | ||
|
||
//Verifica si en el tablero hay reinas que se amenazan entre si | ||
bool Amenaza(bool[,] tablero, int fila, int columna) | ||
{ | ||
|
||
// Verificar si hay alguna reina en la misma fila hacia la izquierda | ||
for (int j = 0; j < columna; j++) | ||
{ | ||
if (tablero[fila, j]) return true; | ||
} | ||
// Diagonal noroeste (arriba a la izquierda) | ||
for (int i = fila - 1, j = columna - 1; i >= 0 && j >= 0; i--, j--) | ||
{ | ||
if (tablero[i, j]) return true; | ||
} | ||
// Diagonal suroeste (abajo a la izquierda) | ||
for (int i = fila + 1, j = columna - 1; i < tablero.GetLength(0) && j >= 0; i++, j--) | ||
{ | ||
if (tablero[i, j]) return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool UbicaReinas(bool[,] tablero, int n) | ||
{ | ||
count++; | ||
// Condición de parada, cuando ya no queden reinas por ubicar | ||
if (n == 0) return true; | ||
|
||
//Quedan n reinas por ubicar | ||
int j = tablero.GetLength(0) - n; | ||
|
||
// Intentar ubicar una reina en la columna j | ||
for (int i = 0; i < tablero.GetLength(0); i++) | ||
{ | ||
// Verificar si la reina en la ubicación no amenaza las anteriores | ||
if (!Amenaza(tablero, i, j)) | ||
{ | ||
// Intento de ubicar la reina en la celda i,j | ||
tablero[i, j] = true; | ||
|
||
//Visualizar el tablero | ||
//VisualizaTablero(tablero); | ||
//Console.WriteLine(); | ||
//Console.ReadLine(); | ||
|
||
// Ver si se puede ubicar las restantes reinas | ||
if (UbicaReinas(tablero, n - 1)) return true; | ||
|
||
// No se pudieron ubicar las restantes. Deshacer la ubicación que se hizo | ||
tablero[i, j] = false; | ||
//VisualizaTablero(tablero); | ||
//Console.WriteLine(); | ||
//Console.ReadLine(); | ||
} | ||
} | ||
return false; // No se pudieron ubicar las reinas en la columna j | ||
} | ||
|
||
#endregion | ||
|
||
Stopwatch crono = new Stopwatch(); | ||
while (true) | ||
{ | ||
count = 0; | ||
Console.Write("\nEntre la cantidad de reinas "); | ||
string s = Console.ReadLine(); | ||
if (s.Length == 0) break; | ||
int n = Int32.Parse(s); | ||
if (n <= 2) Console.WriteLine("Cantidad incorrecta"); | ||
else | ||
{ | ||
var tablero = new bool[n, n]; | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < n; j++) | ||
tablero[i, j] = false; | ||
crono.Restart(); | ||
var result = UbicaReinas(tablero, n); | ||
crono.Stop(); | ||
if (result) | ||
Console.WriteLine("Se ubicaron {0} reinas en {1} ms y {2} llamadas", n, crono.ElapsedMilliseconds, count); | ||
else | ||
Console.WriteLine("No se pueden ubicar {0} reinas", n); | ||
Console.WriteLine("TABLERO FINAL"); | ||
VisualizaTablero(tablero); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>_03_Laberinto</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.