Beispiel #1
0
        private static long Solve(Position startPosition)
        {
            var queue = new Queue<Position>();
            queue.Enqueue(startPosition);

            while (queue.Count != 0)
            {
                var currentPosition = queue.Dequeue();
                available[currentPosition.Row, currentPosition.Col] = false;

                foreach (var nextMovePosition in availableHorseMoves)
                {
                    var nextPosition = currentPosition + nextMovePosition;
                    nextPosition.NumberOfMoves = currentPosition.NumberOfMoves + 1;

                    if (IsPossiblePosition(nextPosition))
                    {
                        if (nextPosition.Row == endPosition.Row && nextPosition.Col == endPosition.Col)
                        {
                            return nextPosition.NumberOfMoves;
                        }

                        if (available[nextPosition.Row, nextPosition.Col])
                        {
                            queue.Enqueue(nextPosition);
                            available[nextPosition.Row, nextPosition.Col] = false;
                        }
                    }
                }
            }

            return -1;
        }
Beispiel #2
0
        private static void ReadInput()
        {
            int n = int.Parse(Console.ReadLine());

            available = new bool[n, n];

            for (int i = 0; i < n; i++)
            {
                var lineAsCharArray = Console.ReadLine().Split(' ');

                for (int j = 0; j < lineAsCharArray.Length; j++)
                {
                    if (lineAsCharArray[j] == "s")
                    {
                        startPosition = new Position() { Row = i, Col = j, NumberOfMoves = 0L };
                    }
                    else if (lineAsCharArray[j] == "e")
                    {
                        endPosition = new Position() { Row = i, Col = j };
                        available[i, j] = true;
                    }
                    else if (lineAsCharArray[j] == "-")
                    {
                        available[i, j] = true;
                    }
                }
            }
        }
Beispiel #3
0
 private static bool IsPossiblePosition(Position position)
 {
     return position.Row >= 0 && position.Col >= 0 && position.Row < available.GetLength(0) && position.Col < available.GetLength(1);
 }