Beispiel #1
0
        static void Main()
        {
            int target   = 265149;
            int gridbase = 515;

            int startLocation = (gridbase - 1) / 2;

            int[,] grid = new int[gridbase, gridbase];
            grid[startLocation, startLocation] = 1;

            SpiralDirection direction = SpiralDirection.Right;
            int             x         = startLocation;
            int             y         = startLocation;

            while (true)
            {
                if (direction == SpiralDirection.Right)
                {
                    x++;
                    if (grid[x, y - 1] == 0)
                    {
                        direction = SpiralDirection.Up;
                    }
                }
                else if (direction == SpiralDirection.Up)
                {
                    y--;
                    if (grid[x - 1, y] == 0)
                    {
                        direction = SpiralDirection.Left;
                    }
                }
                else if (direction == SpiralDirection.Left)
                {
                    x--;
                    if (grid[x, y + 1] == 0)
                    {
                        direction = SpiralDirection.Down;
                    }
                }
                else
                {
                    y++;
                    if (grid[x + 1, y] == 0)
                    {
                        direction = SpiralDirection.Right;
                    }
                }

                grid[x, y] = SumOfAdjacent(new Tuple <int, int>(x, y), grid);
                if (grid[x, y] > target)
                {
                    break;
                }
            }
            Console.WriteLine(grid[x, y]);
            Console.ReadLine();
        }
Beispiel #2
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="system">ParticleSystem that this emitter will add itself to.</param>
        /// <param name="budget">Number of Particles available to this Emitter.</param>
        /// <param name="radius">Radius of the spiral.</param>
        /// <param name="rate">The amount of time in seconds it will take for the spiral to turn 1 revolution.</param>
        /// <param name="direction">Direction of the spiral.</param>
        public SpiralEmitter(ParticleSystem system, int budget, float radius, int rate, SpiralDirection direction)
            : base(system, budget)
        {
            _radius    = radius;
            _curTime   = 0f;
            _increment = 1f / (float)rate;
            _direction = direction;

            AutoResetEvent autoReset     = new AutoResetEvent(false);
            TimerCallback  timerDelegate = new TimerCallback(Tick);

            _timer = new Timer(timerDelegate, autoReset, 0, rate);
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="system">ParticleSystem that this emitter will add itself to.</param>
        /// <param name="budget">Number of Particles available to this Emitter.</param>
        /// <param name="radius">Radius of the spiral.</param>
        /// <param name="rate">The amount of time in seconds it will take for the spiral to turn 1 revolution.</param>
        /// <param name="direction">Direction of the spiral.</param>
        public SpiralEmitter(ParticleSystem system, int budget, float radius, int rate, SpiralDirection direction)
            : base(system, budget)
        {
            _radius = radius;
            _curTime = 0f;
            _increment = 1f / (float)rate;
            _direction = direction;

            AutoResetEvent autoReset = new AutoResetEvent(false);
            TimerCallback timerDelegate = new TimerCallback(Tick);

            _timer = new Timer(timerDelegate, autoReset, 0, rate);
        }
        public SpiralEmitter(uint budget, float radius, byte segments, SpiralDirection dir) : base(budget)
        {
            Radius = radius;
            _dir = dir;
            _segments = (int)segments;

            if (segments < 3) { segments = 3; }

            _angles = new float[segments + 1];

            for (int i = 0; i <= segments; i++)
            {
                _angles[i] = (MathHelper.TwoPi / segments) * i;
            }

            _segment = 0;
        }
        public static int CalculateDistance(int number)
        {
            int distance = 0;

            SpiralDirection currentDirection = SpiralDirection.Right;
            int             currentRing      = 0;
            int             currentX         = 0;
            int             currentY         = 0;

            for (int i = 1; i < number; i++)
            {
                switch (currentDirection)
                {
                case SpiralDirection.Up:
                    if (currentY < currentRing)
                    {
                        currentY++;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Left;
                        currentX--;
                    }
                    break;

                case SpiralDirection.Left:
                    if (currentX > -currentRing)
                    {
                        currentX--;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Down;
                        currentY--;
                    }
                    break;

                case SpiralDirection.Down:
                    if (currentY > -currentRing)
                    {
                        currentY--;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Right;
                        currentX++;
                    }
                    break;

                case SpiralDirection.Right:
                    if (currentX < currentRing)
                    {
                        currentX++;
                    }
                    else
                    {
                        currentRing++;
                        currentX         = currentRing;
                        currentDirection = SpiralDirection.Up;
                    }
                    break;
                }
            }

            distance = Math.Abs(currentX) + Math.Abs(currentY);

            return(distance);
        }
        public static int GetLargerThan(int number)
        {
            int currentNumber = 1;

            _numbers = new Dictionary <Tuple <int, int>, int>();

            SpiralDirection currentDirection = SpiralDirection.Right;
            int             currentRing      = 0;
            int             currentX         = 0;
            int             currentY         = 0;

            while (currentNumber <= number)
            {
                // Add current number to dictionary.
                _numbers.Add(new Tuple <int, int>(currentX, currentY), currentNumber);

                switch (currentDirection)
                {
                case SpiralDirection.Up:
                    if (currentY < currentRing)
                    {
                        currentY++;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Left;
                        currentX--;
                    }
                    break;

                case SpiralDirection.Left:
                    if (currentX > -currentRing)
                    {
                        currentX--;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Down;
                        currentY--;
                    }
                    break;

                case SpiralDirection.Down:
                    if (currentY > -currentRing)
                    {
                        currentY--;
                    }
                    else
                    {
                        currentDirection = SpiralDirection.Right;
                        currentX++;
                    }
                    break;

                case SpiralDirection.Right:
                    if (currentX < currentRing)
                    {
                        currentX++;
                    }
                    else
                    {
                        currentRing++;
                        currentX         = currentRing;
                        currentDirection = SpiralDirection.Up;
                    }
                    break;
                }

                currentNumber = GetSumOfNeighborsForCurrentNumber(currentX, currentY);
            }

            return(currentNumber);
        }
Beispiel #7
0
 private void FillSpiralStep(ref int x, ref int y, ref SpiralDirection direction, ref int minX, ref int minY, ref int maxX, ref int maxY)
 {
     switch (direction)
     {
         case SpiralDirection.Left:
             x -= 1;  // движение влево
             if (x < minX)
             { // проверка выхода за заполненную центральную часть слева
                 direction = SpiralDirection.Up; // меняем направление
                 minX = x; // увеличиваем заполненную часть влево
             }
             break;
         case SpiralDirection.Up:  // движение вверх проверка сверху
             y -= 1;
             if (y < minY)
             {
                 direction = SpiralDirection.Right;
                 minY = y;
             }
             break;
         case SpiralDirection.Right:  // движение вправо проверка справа
             x += 1;
             if (x > maxX)
             {
                 direction = SpiralDirection.Down;
                 maxX = x;
             }
             break;
         case SpiralDirection.Down:  // движение вниз проверка снизу
             y += 1;
             if (y > maxY)
             {
                 direction = SpiralDirection.Left;
                 maxY = y;
             }
             break;
     }
 }