Exemple #1
0
        public static void Driver()
        {
            int boardSize       = 8;
            var sourceNode      = new ChessNode(0, 7);
            var destinationNode = new ChessNode(7, 0);

            int result = FindShortestPathUsingBFS(sourceNode, destinationNode, boardSize);

            Console.WriteLine($"Minimum Steps required : {result}");
        }
Exemple #2
0
        private static int FindShortestPathUsingBFS(ChessNode sourceNode, ChessNode destinationNode, int boardSize)
        {
            if (sourceNode.X == destinationNode.X && sourceNode.Y == destinationNode.Y)
            {
                return(0);
            }

            bool[,] visited = new bool[boardSize, boardSize];

            Queue <ChessNode> queue = new Queue <ChessNode>();

            queue.Enqueue(sourceNode);

            //Next possible 8 moves for a knight

            int[] ROWS = { -1, -1, -2, -2, 1, 1, 2, 2 };
            int[] COLS = { -2, 2, -1, 1, -2, 2, -1, 1 };

            while (queue.Count > 0)
            {
                var currentNode = queue.Dequeue();
                if (currentNode.X == destinationNode.X && currentNode.Y == destinationNode.Y)
                {
                    return(currentNode.Distance);
                }

                for (int i = 0; i < 8; i++)
                {
                    var nextX = currentNode.X + ROWS[i];
                    var nextY = currentNode.Y + COLS[i];

                    if (IsSafeAndValid(nextX, nextY, boardSize, visited))
                    {
                        visited[nextX, nextY] = true;
                        var nextNode = new ChessNode(nextX, nextY, currentNode.Distance + 1);
                        queue.Enqueue(nextNode);
                    }
                }
            }

            return(int.MaxValue);
        }