Exemple #1
0
        private void reconstructPath(MatrixCell destinationCell, List <PathItem> path)
        {
            List <MatrixCell> pathTodes = new List <MatrixCell>();
            var pathItem = path.FirstOrDefault(x => x.Cell == destinationCell);

            while (pathItem != null)
            {
                pathTodes.Add(pathItem.Cell);
                pathItem = path.FirstOrDefault(x => x.Cell == pathItem.ParentCell);
            }
            pathTodes.Reverse();
            foreach (var cell in pathTodes)
            {
                Console.WriteLine($"{cell.x}, {cell.y}");
            }
        }
Exemple #2
0
        public void PathBFS(int[,] matrix)
        {
            var lenX = matrix.GetLength(0);
            var lenY = matrix.GetLength(1);

            var visited = new bool[lenX, lenY];
            Queue <QueueItemMatrix> queue = new Queue <QueueItemMatrix>();
            var path = new List <PathItem>();

            var startQueueItem = new QueueItemMatrix(new MatrixCell(0, 0), 0);

            queue.Enqueue(startQueueItem);

            path.Add(new PathItem(startQueueItem.Cell, null));

            MatrixCell destinationCell = null;

            while (queue.Any())
            {
                var cell = queue.Dequeue();

                visited[cell.Cell.x, cell.Cell.y] = true;

                if (matrix[cell.Cell.x, cell.Cell.y] == 0)
                {
                    destinationCell = cell.Cell;
                    break;
                }

                var list = GetAdjucentCell(matrix, cell, visited, lenX, lenY);
                foreach (var item in list)
                {
                    path.Add(new PathItem(item.Cell, cell.Cell));
                    queue.Enqueue(item);
                }
            }

            if (destinationCell != null)
            {
                reconstructPath(destinationCell, path);
            }
            else
            {
                Console.WriteLine("No Path found");
            }
        }
Exemple #3
0
 public PathItem(MatrixCell cell, MatrixCell parentCell)
 {
     Cell       = cell;
     ParentCell = parentCell;
 }
Exemple #4
0
 public QueueItemMatrix(MatrixCell cell, int level)
 {
     Cell  = cell;
     Level = level;
 }