Advertisement
BSO90

Spiral Matrix

May 12th, 2021
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Additional_NON_Required_Advanced_Problem_Spiral_Matrix
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Write("Enter a number: ");
  10.             byte n = byte.Parse(Console.ReadLine());
  11.  
  12.             int[,] matrix = new int[n, n];
  13.             sbyte row = 0;
  14.             sbyte col = -1;
  15.             string direction = "right";
  16.  
  17.             Console.WriteLine();
  18.             for (int i = 1; i <= n * n; i++)
  19.             {
  20.                 if (direction == "right")
  21.                 {
  22.                     if (matrix[row, ++col] == 0)
  23.                     {
  24.                         matrix[row, col] = i;
  25.                     }
  26.  
  27.                     if (col + 1 >= n || matrix[row, col + 1] != 0)
  28.                     {
  29.                         direction = "down";
  30.                     }
  31.                 }
  32.  
  33.                 else if (direction == "down")
  34.                 {
  35.                     if (matrix[++row, col] == 0)
  36.                     {
  37.                         matrix[row, col] = i;
  38.                     }
  39.                     if (row + 1 >= n || matrix[row + 1, col] != 0)
  40.                     {
  41.                         direction = "left";
  42.                     }
  43.                 }
  44.  
  45.                 else if (direction == "left")
  46.                 {
  47.                     if (matrix[row, --col] == 0)
  48.                     {
  49.                         matrix[row, col] = i;
  50.                     }
  51.                     if (col - 1 < 0 || matrix[row, col - 1] != 0)
  52.                     {
  53.                         direction = "up";
  54.                     }
  55.                 }
  56.  
  57.                 else if (direction == "up")
  58.                 {
  59.                     if (matrix[--row, col] == 0)
  60.                     {
  61.                         matrix[row, col] = i;
  62.                     }
  63.                     if (row - 1 < 0 || matrix[row - 1, col] != 0)
  64.                     {
  65.                         direction = "right";
  66.                     }
  67.                        
  68.                 }
  69.             }
  70.  
  71.             for (int i = 0; i < matrix.GetLength(0); i++)
  72.             {
  73.                 for (int j = 0; j < matrix.GetLength(1); j++)
  74.                 {
  75.                     Console.Write("{0,4}", matrix[i, j] + " ");
  76.                 }
  77.  
  78.                 Console.WriteLine();
  79.             }
  80.         }
  81.     }
  82. }
  83.                
  84.  
  85.            
  86.                    
  87.                
  88.            
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement