Beispiel #1
0
        public void FindPathsQ(Cell cell)
        {
            this.queue.Enqueue(cell);

            while (this.queue.Count > 0)
            {
                Cell currentCell = this.queue.Dequeue();
                int row = currentCell.Row;
                int col = currentCell.Col;
                int dist = currentCell.Distance;
                matrix[row, col] = dist.ToString();

                for (int i = 0; i < dir.GetLength(0); i++)
                {
                    var nextCell = new Cell(
                        row + dir[i, 0],
                        col + dir[i, 1],
                        dist + 1);

                    if (isValid(nextCell))
                    {
                        queue.Enqueue(nextCell);
                    }
                }
            }
        }
Beispiel #2
0
 public void MarkUnreachableCells(Cell startCell)
 {
     for (int row = 0; row < this.matrix.GetLength(0); row++)
     {
         for (int col = 0; col < this.matrix.GetLength(1); col++)
         {
             if (this.matrix[row, col] == "0")
             {
                 this.matrix[row, col] = "u";
             }
         }
     }
     this.matrix[startCell.Row, startCell.Col] = "*";
 }
Beispiel #3
0
        public static void Main()
        {
            // We are given a matrix of passable and non-passable cells.
            // Write a recursive program for finding all paths between two cells in the matrix.

            string[,] matrix =
            {
                { "0", "0", "0", "x", "0", "x" },
                { "0", "x", "0", "x", "0", "x" },
                { "0", "*", "x", "0", "x", "0" },
                { "0", "x", "0", "0", "0", "0" },
                { "0", "0", "0", "x", "x", "0" },
                { "0", "0", "0", "x", "e", "0" }
            };

            var lab = new Labyrinth(matrix);
            Cell startCell = new Cell(2, 1, 0);
            lab.FindPathsQ(startCell);
            lab.MarkUnreachableCells(startCell);
            Console.WriteLine("All Paths");
            lab.PrintMatrix();
        }
Beispiel #4
0
 private bool isValid(Cell nextCell)
 {
     return nextCell.Row < this.matrix.GetLength(0) && nextCell.Row >= 0 &&
             nextCell.Col < this.matrix.GetLength(1) && nextCell.Col >= 0 &&
             this.matrix[nextCell.Row, nextCell.Col] == "0";
 }