Ejemplo n.º 1
0
        static void PlayAllHorseMovements()
        {
            chess = new int[height, width];
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    chess[i, j] = 0;
                }
            }
            chess[startY, startX]++;

            var startPosition = new Position() { X = startX, Y = startY };
            queue = new Queue<Position>();
            queue.Enqueue(startPosition);
            while (queue.Count > 0)
            {
                var currentPos = queue.Dequeue();
                TryDirection(queue, currentPos, +1, -2);
                TryDirection(queue, currentPos, -1, -2);
                TryDirection(queue, currentPos, +2, -1);
                TryDirection(queue, currentPos, +2, +1);
                TryDirection(queue, currentPos, +1, +2);
                TryDirection(queue, currentPos, -1, +2);
                TryDirection(queue, currentPos, -2, +1);
                TryDirection(queue, currentPos, -2, -1);
            }
        }
Ejemplo n.º 2
0
        static void TryDirection(Queue<Position> queue, Position currentPos, int deltaX, int deltaY)
        {
            int newX = currentPos.X + deltaX;
            int newY = currentPos.Y + deltaY;
            if (newX >= 0 && newX < width && newY >= 0 && newY < height
                && chess[newY, newX] == 0)
            {
                chess[newY, newX] = (chess[currentPos.Y, currentPos.X] + 1);
                Position newPos = new Position()
                {
                    X = newX,
                    Y = newY
                };

                queue.Enqueue(newPos);
            }
        }