Matrix arrangement issues in php

前端 未结 6 1010
闹比i
闹比i 2021-02-20 13:07

I would like to know some solutions to such a problem.

It is given a number lets say 16 and you have to arrange a matrix this way

1  2  3  4
12 13 14 5
         


        
6条回答
  •  醉酒成梦
    2021-02-20 14:12

    Looks like the snake game might work. Track a direction vector, and turn right 90 degrees every time you hit a side or a populated square. The tail keeps extending indefinitely :)

    Edit : Snakey v0.1 in C#. Works for non square grids too ;)

    using System;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public enum Direction
            {
                Up,
                Down,
                Left,
                Right
            }
    
            static void Main(string[] args)
            {
                int[,] maze;
    
                Direction currentDirection = Direction.Right;
    
                bool totallyStuck = false;
                bool complete = false;
                int currentX = 0;
                int currentY = 0;
                int boundX = 4;
                int boundY = 5;
                int currentNumber = 1;
                int stuckCounter = 0;
                bool placeNumber = true;
    
                maze = new int[boundY, boundX];
    
                while ((!totallyStuck) && (!complete))
                {
                    if (placeNumber)
                    {
                        maze[currentY, currentX] = currentNumber;
                        currentNumber++;
                        stuckCounter = 0;
                    }
    
                    switch (currentDirection)
                    {
                        case Direction.Right:
                            // Noted short Circuit Bool Evan
                            if ((currentX + 1 < boundX) && (maze[currentY, currentX + 1] == 0))
                            {
                                placeNumber = true;
                                currentX++;
                                stuckCounter = 0;
                            }
                            else
                            {
                                placeNumber = false;
                                stuckCounter++;
                            }
    
                            break;
                        case Direction.Left:
                            if ((currentX - 1 >= 0) && (maze[currentY, currentX - 1] == 0))
                            {
                                placeNumber = true;
                                currentX--;
                                stuckCounter = 0;
                            }
                            else
                            {
                                placeNumber = false;
                                stuckCounter++;
                            }
                            break;
                        case Direction.Down:
                            if ((currentY + 1 < boundY) && (maze[currentY + 1, currentX] == 0))
                            {
                                placeNumber = true;
                                currentY++;
                                stuckCounter = 0;
                            }
                            else
                            {
                                placeNumber = false;
                                stuckCounter++;
                            }
                            break;
                        case Direction.Up:
                            if ((currentY - 1 >= 0) && (maze[currentY - 1, currentX] == 0))
                            {
                                placeNumber = true;
                                currentY--;
                                stuckCounter = 0;
                            }
                            else
                            {
                                placeNumber = false;
                                stuckCounter++;
                            }
                            break;
                    }
    
                    // Is Snake stuck? If so, rotate 90 degs right
                    if (stuckCounter == 1)
                    {
                        switch (currentDirection)
                        {
                            case Direction.Right:
                                currentDirection = Direction.Down;
                                break;
                            case Direction.Down:
                                currentDirection = Direction.Left;
                                break;
                            case Direction.Left:
                                currentDirection = Direction.Up;
                                break;
                            case Direction.Up:
                                currentDirection = Direction.Right;
                                break;
                        }
                    }
                    else if (stuckCounter > 1)
                    {
                        totallyStuck = true;
                    }
                }
    
                // Draw final maze
                for (int y = 0; y < boundY; y++)
                {
                    for (int x = 0; x < boundX; x++)
                    {
                        Console.Write(string.Format("{0:00} ",maze[y, x]));
                    }
                    Console.Write("\r\n");
                }
            }
        }
    }
    

提交回复
热议问题