Ejercicio 02
Ejercicio 02
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);
}
}
}
}