コード例 #1
0
        public static int[,] GenerateRotatingWalkMatrix(int n)
        {
            if (n < 1 || n > 100)
            {
                throw new ArgumentOutOfRangeException(
                    "The size of the matrix should be positive integer number " +
                    "in the range [1..100]!");
            }

            int[,] outputMatrix = new int[n, n];

            int number = 1;
            Point currentCell = new Point() { Row = 0, Col = 0 };
            Direction currentDir = new Direction() { DirX = 1, DirY = 1 };
            FillMatrixWhilePossible(n, outputMatrix, ref number, currentCell, ref currentDir);

            number++;

            currentCell = GetFirstEmptyCell(outputMatrix);
            if (currentCell != null)
            {
                currentDir.DirX = 1;
                currentDir.DirY = 1;
                FillMatrixWhilePossible(n, outputMatrix, ref number, currentCell, ref currentDir);
            }

            return outputMatrix;
        }
コード例 #2
0
        static Direction GetNextDirection(Direction currentDirection)
        {
            int[] dirX = { 1, 1, 1, 0, -1, -1, -1, 0 };
            int[] dirY = { 1, 0, -1, -1, -1, 0, 1, 1 };
            for (int i = 0; i < dirX.Length; i++)
            {
                if (dirX[i] == currentDirection.DirX && dirY[i] == currentDirection.DirY)
                {
                    Direction nextDirection = new Direction()
                    {
                        DirX = dirX[(i + 1) % dirX.Length],
                        DirY = dirY[(i + 1) % dirX.Length]
                    };
                    return nextDirection;
                }
            }

            throw new InvalidOperationException(
                String.Format(
                    "Could not find the next direction: dirX={0}, dirY={1}",
                    currentDirection.DirX,
                    currentDirection.DirY));
        }
コード例 #3
0
 private static void FillMatrixWhilePossible(int n, int[,] outputMatrix, ref int number, Point currentCell, ref Direction currentDir)
 {
     while (true) //malko e kofti tova uslovie, no break-a raboti 100% :)
     {
         outputMatrix[currentCell.Row, currentCell.Col] = number;
         if (!CheckDirections(outputMatrix, currentCell.Row, currentCell.Col))
         {
             break;      // prekusvame ako sme se zadunili
         }
         while (IsNextCellAvailable(n, outputMatrix, currentCell, currentDir))
         {
             currentDir = GetNextDirection(currentDir);
         }
         currentCell.Row += currentDir.DirX;
         currentCell.Col += currentDir.DirY;
         number++;
     }
 }
コード例 #4
0
 private static bool IsNextCellAvailable(
     int n, int[,] matrix, Point cell, Direction dir)
 {
     return cell.Row + dir.DirX >= n || cell.Row + dir.DirX < 0 ||
         cell.Col + dir.DirY >= n || cell.Col + dir.DirY < 0 ||
         matrix[cell.Row + dir.DirX, cell.Col + dir.DirY] != 0;
 }