Ejemplo n.º 1
0
        private static string[,] SolveLabyrinth(string[,] labyrinth)
        {
            int startX = 0;
            int startY = 0;

            if (GetStartingPoint(labyrinth, out startX, out startY))
            {
                Queue <LabyrinthElement> queue = new Queue <LabyrinthElement>();
                labyrinth[startX, startY] = "0";
                LabyrinthElement element = new LabyrinthElement(startX, startY, int.Parse(labyrinth[startX, startY]));
                queue.Enqueue(element);

                while (queue.Count > 0)
                {
                    LabyrinthElement newElement = new LabyrinthElement(queue.Dequeue());
                    labyrinth[newElement.X, newElement.Y] = newElement.Value.ToString();
                    newElement.Value++;

                    if (((newElement.X - 1) >= 0) &&
                        (labyrinth[newElement.X - 1, newElement.Y] == "0"))
                    {
                        LabyrinthElement firstElement = new LabyrinthElement(newElement.X - 1, newElement.Y, newElement.Value);
                        queue.Enqueue(firstElement);
                    }
                    if ((newElement.X + 1 < labyrinth.GetLength(0)) &&
                        (labyrinth[newElement.X + 1, newElement.Y] == "0"))
                    {
                        LabyrinthElement secondElement = new LabyrinthElement(newElement.X + 1, newElement.Y, newElement.Value);
                        queue.Enqueue(secondElement);
                    }
                    if ((newElement.Y - 1 >= 0) &&
                        (labyrinth[newElement.X, newElement.Y - 1] == "0"))
                    {
                        LabyrinthElement thirdElement = new LabyrinthElement(newElement.X, newElement.Y - 1, newElement.Value);
                        queue.Enqueue(thirdElement);
                    }
                    if ((newElement.Y + 1 < labyrinth.GetLength(1)) &&
                        (labyrinth[newElement.X, newElement.Y + 1] == "0"))
                    {
                        LabyrinthElement fourthElement = new LabyrinthElement(newElement.X, newElement.Y + 1, newElement.Value);
                        queue.Enqueue(fourthElement);
                    }
                }
                labyrinth[startX, startY] = "*";

                FindUnreachableCells(labyrinth);
            }
            else
            {
                throw new ArgumentException("No entry point");
            }

            return(labyrinth);
        }
Ejemplo n.º 2
0
        private static string[,] SolveLabyrinth(string[,] labyrinth)
        {
            int startX = 0;
            int startY = 0;

            if (GetStartingPoint(labyrinth, out startX, out startY))
            {
                Queue<LabyrinthElement> queue = new Queue<LabyrinthElement>();
                labyrinth[startX, startY] = "0";
                LabyrinthElement element = new LabyrinthElement(startX, startY, int.Parse(labyrinth[startX, startY]));
                queue.Enqueue(element);

                while (queue.Count > 0)
                {
                    LabyrinthElement newElement = new LabyrinthElement(queue.Dequeue());
                    labyrinth[newElement.X, newElement.Y] = newElement.Value.ToString();
                    newElement.Value++;

                    if (((newElement.X - 1) >= 0) &&
                        (labyrinth[newElement.X - 1, newElement.Y] == "0"))
                    {
                        LabyrinthElement firstElement = new LabyrinthElement(newElement.X - 1, newElement.Y, newElement.Value);
                        queue.Enqueue(firstElement);
                    }
                    if ((newElement.X + 1 < labyrinth.GetLength(0)) &&
                        (labyrinth[newElement.X + 1, newElement.Y] == "0"))
                    {
                        LabyrinthElement secondElement = new LabyrinthElement(newElement.X + 1, newElement.Y, newElement.Value);
                        queue.Enqueue(secondElement);
                    }
                    if ((newElement.Y - 1 >= 0) &&
                        (labyrinth[newElement.X, newElement.Y - 1] == "0"))
                    {
                        LabyrinthElement thirdElement = new LabyrinthElement(newElement.X, newElement.Y - 1, newElement.Value);
                        queue.Enqueue(thirdElement);
                    }
                    if ((newElement.Y + 1 < labyrinth.GetLength(1)) &&
                        (labyrinth[newElement.X, newElement.Y + 1] == "0"))
                    {
                        LabyrinthElement fourthElement = new LabyrinthElement(newElement.X, newElement.Y + 1, newElement.Value);
                        queue.Enqueue(fourthElement);
                    }
                }
                labyrinth[startX, startY] = "*";

                FindUnreachableCells(labyrinth);
            }
            else
            {
                throw new ArgumentException("No entry point");
            }

            return labyrinth;
        }
Ejemplo n.º 3
0
 public LabyrinthElement(LabyrinthElement element)
 {
     this.X     = element.X;
     this.Y     = element.Y;
     this.Value = element.Value;
 }
Ejemplo n.º 4
0
 public LabyrinthElement(LabyrinthElement element)
 {
     this.X = element.X;
     this.Y = element.Y;
     this.Value = element.Value;
 }