Advertisement
AlexPopov

Untitled

Dec 15th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2.  
  3. class SpiralMatrix
  4. {
  5.     public static void Main()
  6.     {
  7.         //Initiate a variable for the size of the matrix
  8.         int n;
  9.        
  10.         //User input
  11.         Console.Write("Input an integer number in the interval [1;19]: ");
  12.        
  13.         //A variable to check for valid input
  14.         bool valid = Int32.TryParse(Console.ReadLine(), out n);
  15.        
  16.         //Input validation
  17.         if (valid && (n > 0 && n < 20))
  18.         {
  19.             //Initiation of the matrix
  20.             int[,] num = new int[n, n];
  21.            
  22.             //Initiation of coordinates and direction
  23.             int x = 0, y = 0, direction = 0;
  24.  
  25.             //For-loop for filling up the matrix
  26.             for (int i = 0; i < n * n; i++)
  27.             {
  28.                
  29.                 //Write on a particular position in the matrix the value of the counter of the for-loop. The starting position is (0,0) and the first value is 1.
  30.                 num[x, y] = i + 1;
  31.  
  32.                 //Depending on the direction of movement there are 4 different cases.
  33.                 switch (direction)
  34.                 {
  35.                     //Direction down
  36.                     case 0:
  37.                         //If x is lower the the maxXBound and the position is empty increase x by 1 and start the loop again. This will continue until row 1 is full
  38.                         if (x < n - 1 && num[x + 1, y] == 0)
  39.                         {
  40.                             x++;
  41.                         }
  42.                         //When row 1 is full the direction changes:
  43.                         else
  44.                         {
  45.                             direction = (++direction) % 4;
  46.                             y++;
  47.                         }
  48.                         break;
  49.                    
  50.                     //Direction right
  51.                     case 1:
  52.                         if (y < n - 1 && num[x, y + 1] == 0)
  53.                         {
  54.                             y++;
  55.                         }
  56.                         else
  57.                         {
  58.                             direction = (++direction) % 4;
  59.                             x--;
  60.                         }
  61.                         break;
  62.                    
  63.                     //Direction up
  64.                     case 2:
  65.                         if (x > 0 && num[x - 1, y] == 0)
  66.                         {
  67.                             x--;
  68.                         }
  69.                         else
  70.                         {
  71.                             direction = (++direction) % 4;
  72.                             y--;
  73.                         }
  74.                         break;
  75.                    
  76.                     //Direction left
  77.                     case 3:
  78.                         if (y > 0 && num[x, y - 1] == 0)
  79.                         {
  80.                             y--;
  81.                         }
  82.                         else
  83.                         {
  84.                             direction = (++direction) % 4;
  85.                             x++;
  86.                         }
  87.                         break;
  88.                     default:
  89.                         Console.WriteLine("Invalid direction value!");
  90.                         break;
  91.                 }
  92.             }
  93.             int align = ((n * n).ToString()).Length;
  94.             for (int i = 0; i < n; i++)
  95.             {
  96.                 for (int j = 0; j < n; j++)
  97.                 {
  98.                     Console.Write("{0}", ((num[j, i].ToString()).PadRight(align / 2)).PadLeft(align + 1));
  99.                 }
  100.                 Console.WriteLine();
  101.             }
  102.         }
  103.         else
  104.         {
  105.             Console.WriteLine("Invalid input data!");
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement