Advertisement
Guest User

Untitled

a guest
Mar 7th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. namespace Matrix
  2. {
  3.     using System;
  4.  
  5.     class WalkInMatrix
  6.     {
  7.         private const int NumberOfPossibleDirections = 8;
  8.  
  9.         static void changeDirection(ref int horizontalDirection, ref int verticalDirection)
  10.         {
  11.             int[] horizontalDirections = { 1, 1, 1, 0, -1, -1, -1, 0 };
  12.             int[] diagonalDirections = { 1, 0, -1, -1, -1, 0, 1, 1 };
  13.             int changeDirectionTimesCount = 0;
  14.  
  15.             for (int directionsCount = 0; directionsCount < NumberOfPossibleDirections; directionsCount++)
  16.             {
  17.                 if (horizontalDirections[directionsCount] == horizontalDirection &&
  18.                     diagonalDirections[directionsCount] == verticalDirection)
  19.                 {
  20.                     changeDirectionTimesCount = directionsCount;
  21.                     break;
  22.                 }
  23.             }
  24.            
  25.             if (changeDirectionTimesCount == 7)
  26.             {
  27.                 horizontalDirection = horizontalDirections[0];
  28.                 verticalDirection = diagonalDirections[0];
  29.                
  30.                 return;
  31.             }
  32.            
  33.             horizontalDirection = horizontalDirections[changeDirectionTimesCount + 1];
  34.             verticalDirection = diagonalDirections[changeDirectionTimesCount + 1];
  35.         }
  36.  
  37.         static bool CheckNextCell(int[,] matrix, int x, int y)
  38.         {
  39.             int[] horizontalDirections = { 1, 1, 1, 0, -1, -1, -1, 0 };
  40.             int[] diagonalDirections = { 1, 0, -1, -1, -1, 0, 1, 1 };
  41.          
  42.             for (int i = 0; i < NumberOfPossibleDirections; i++)
  43.             {
  44.                 if (x + horizontalDirections[i] >= matrix.GetLength(0) || x + horizontalDirections[i] < 0)
  45.                 {
  46.                     horizontalDirections[i] = 0;
  47.                 }
  48.  
  49.                 if (y + diagonalDirections[i] >= matrix.GetLength(0) || y + diagonalDirections[i] < 0)
  50.                 {
  51.                     diagonalDirections[i] = 0;
  52.                 }
  53.             }
  54.            
  55.             for (int i = 0; i < NumberOfPossibleDirections; i++)
  56.             {
  57.                 if (matrix[x + horizontalDirections[i], y + diagonalDirections[i]] == 0)
  58.                 {
  59.                     return true;
  60.                 }
  61.             }
  62.  
  63.             return false;
  64.         }
  65.  
  66.         static void FindNextCell(int[,] matrix, out int x, out int y)
  67.         {
  68.             x = 0;
  69.             y = 0;
  70.  
  71.             for (int i = 0; i < matrix.GetLength(0); i++)
  72.             {
  73.                 for (int j = 0; j < matrix.GetLength(0); j++)
  74.                 {
  75.                     if (matrix[i, j] == 0)
  76.                     {
  77.                         x = i;
  78.                         y = j;
  79.  
  80.                         return;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             Console.WriteLine("Enter a positive number ");
  89.             string input = Console.ReadLine();
  90.             int n = 0;
  91.  
  92.             while (!int.TryParse(input, out n) || n < 0 || n > 100)
  93.             {
  94.                 Console.WriteLine("You haven't entered a correct positive number");
  95.                 input = Console.ReadLine();
  96.             }
  97.  
  98.             int[,] matrix = new int[n, n];
  99.             int row = 0;
  100.             int col = 0;
  101.             int matrixSquare = 1;
  102.             int horizontalDirection = 1;
  103.             int diagonalDirection = 1;
  104.  
  105.             SetupMatrix(matrix, row, col, matrixSquare, horizontalDirection, diagonalDirection, n);
  106.  
  107.             FindNextCell(matrix, out row, out col);
  108.            
  109.             if (row != 0 && col != 0)
  110.             {
  111.                 horizontalDirection = 1;
  112.                 diagonalDirection = 1;
  113.                
  114.                 SetupMatrix(matrix, row, col, matrixSquare, horizontalDirection, diagonalDirection, n);
  115.             }
  116.  
  117.             for (int i = 0; i < n; i++)
  118.             {
  119.                 for (int j = 0; j < n; j++)
  120.                 {
  121.                     Console.Write("{0,3}", matrix[i, j]);
  122.                 }
  123.  
  124.                 Console.WriteLine();
  125.             }
  126.         }
  127.  
  128.         private static void SetupMatrix(int[,] matrix, int row, int col, int matrixSquare, int horizontalDirection,
  129.             int diagonalDirection, int n)
  130.         {
  131.             while (true)
  132.             {
  133.                 matrix[row, col] = matrixSquare;
  134.  
  135.                 if (!CheckNextCell(matrix, row, col))
  136.                 {
  137.                     break;
  138.                 }
  139.                 if (row + horizontalDirection >= n || row + horizontalDirection < 0 || col + diagonalDirection >= n ||
  140.                     col + diagonalDirection < 0 || matrix[row + horizontalDirection, col + diagonalDirection] != 0)
  141.                 {
  142.                     while ((row + horizontalDirection >= n || row + horizontalDirection < 0 || col + diagonalDirection >= n ||
  143.                         col + diagonalDirection < 0 || matrix[row + horizontalDirection, col + diagonalDirection] != 0))
  144.                     {
  145.                         changeDirection(ref horizontalDirection, ref diagonalDirection);
  146.                     }
  147.                 }
  148.  
  149.                 row += horizontalDirection;
  150.                 col += diagonalDirection;
  151.                 matrixSquare++;
  152.             }
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement