0% found this document useful (0 votes)
43 views3 pages

Ejercicio 02

This C# program generates a 2D array representing a grid with random numbers. It allows the user to specify the grid size and then select a cell to "collect" from, recursively collecting garbage from neighboring cells and tracking the total amount. The grid is displayed before and after collection, and the total garbage collected is reported.

Uploaded by

aigro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Ejercicio 02

This C# program generates a 2D array representing a grid with random numbers. It allows the user to specify the grid size and then select a cell to "collect" from, recursively collecting garbage from neighboring cells and tracking the total amount. The grid is displayed before and after collection, and the total garbage collected is reported.

Uploaded by

aigro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX or read online on Scribd
You are on page 1/ 3

/* Fecha de Creación : 07/09/10

* Fecha de Publicación : 08/09/10


* Creado por : IAGRO
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Ejercicio02
{
class Program
{
static int[,] Tabla;
static void Main(string[] args)
{
Console.Write("n : ");
int n = int.Parse(Console.ReadLine());
Console.Write("m : ");
int m = int.Parse(Console.ReadLine());
GenerarTabla(n, m);
MostrarTabla(n, m);
Console.Write("fila : ");
int fila = int.Parse(Console.ReadLine());
Console.Write("columna : ");
int columna = int.Parse(Console.ReadLine());
int Basura=0;
Recolectar(n, m, fila, columna, ref Basura);
Console.WriteLine("Basura recolectada : " + Basura);
Console.ReadKey();
}
static void MostrarTabla(int n, int m)
{
for (int fila = 0; fila < n; fila++)
{
for (int columna = 0; columna < m; columna++)
{
int k = Tabla[fila, columna];
if (k == -1)
{
k = 0;
Console.ForegroundColor = ConsoleColor.Green;
}
else
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(k);
}
Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.Cyan;
}
static void GenerarTabla(int n,int m)
{
Tabla = new int[n, m];
Random R = new Random();
R.Next();
for (int fila = 0; fila < n; fila++)
{
for (int columna = 0; columna < m; columna++)
{
Tabla[fila, columna] = R.Next(9) + 1;
}
}
ArrayList X = new ArrayList();
ArrayList Y = new ArrayList();
for (int fila = 0; fila < n; fila++)
{
for (int columna = 0; columna < m; columna++)
{
X.Add(fila);
Y.Add(columna);
}
}
for (int Indice = 0; Indice < (n * m / 2); Indice++)
{
int iesimo=R.Next(X.Count);
int x =(int) X[iesimo];
int y =(int) Y[iesimo];
X.RemoveAt(iesimo);
Y.RemoveAt(iesimo);
Tabla[x, y] = 0;
}
}
static void Recolectar(int n,int m,int fila, int columna, ref int
Basura)
{
if (Tabla[fila, columna] > 0)
{
Basura += Tabla[fila, columna];
Tabla[fila, columna] = -1;
MostrarTabla(n, m);
Console.WriteLine(Basura);
Console.ReadKey();
if (fila +1< n)
Recolectar(n, m, fila + 1, columna, ref Basura);
if (fila > 0)
Recolectar(n, m, fila - 1, columna, ref Basura);
if (columna +1 < m)
Recolectar(n, m, fila, columna + 1, ref Basura);
if (columna > 0)
Recolectar(n, m, fila , columna - 1, ref Basura);
}
}
}
}

You might also like