Advertisement
kuruku

SpiralMatrix

Jul 19th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2.  
  3. class SpiralMatrix
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("x = ");
  8.         int x = int.Parse(Console.ReadLine());
  9.         Console.Write("y = ");
  10.         int y = int.Parse(Console.ReadLine());
  11.  
  12.         Console.WriteLine();
  13.         int[,] arrInt = new int[y, x];
  14.  
  15.         /* Generate matrix [x,y] */
  16.  
  17.         for (int a = 0; a < y; a++)
  18.         {
  19.             for (int b = 0; b < x; b++)
  20.             {
  21.                 arrInt[a, b] = x * a + b + 1;
  22.             }
  23.         }
  24.         /* Print matrix [x,y] */
  25.  
  26.         for (int a = 0; a < y; a++)
  27.         {
  28.             for (int b = 0; b < x; b++)
  29.             {
  30.                 Console.Write("{0,5}", arrInt[a, b]);
  31.             }
  32.             Console.WriteLine();
  33.         }
  34.  
  35.         Console.WriteLine();
  36.  
  37.         /* Print matrix in spiral form */
  38.  
  39.         byte direction = 0; /*0-right, 1-down, 2-left, 3-right*/
  40.         int row = 0, col = 0;
  41.         int matrixIndex = 0;
  42.  
  43.         for (int k = 0; k < y; k++)
  44.         {
  45.             for (int c = 0; c < x; c++)
  46.             {
  47.  
  48.                 Console.Write(arrInt[row, col] + " ");
  49.                 if (direction == 0) //right
  50.                 {
  51.                     if (col + 1 + matrixIndex >= x)
  52.                     {
  53.                         direction = 1;
  54.                         row++;
  55.                     }
  56.                     else
  57.                     {
  58.                         col++;
  59.                     }
  60.                 }
  61.                 else if (direction == 1) //down
  62.                 {
  63.                     if (row + 1 + matrixIndex >= y)
  64.                     {
  65.                         direction = 2;
  66.                         col--;
  67.                     }
  68.                     else
  69.                     {
  70.                         row++;
  71.                     }
  72.                 }
  73.                 else if (direction == 2) //left
  74.                 {
  75.                     if (col - 1 - matrixIndex < 0)
  76.                     {
  77.                         direction = 3;
  78.                         row--;
  79.                     }
  80.                     else
  81.                     {
  82.                         col--;
  83.                     }
  84.                 }
  85.                 else if (direction == 3) //up
  86.                 {
  87.                     if (matrixIndex + 1 == row)
  88.                     {
  89.                         matrixIndex++;
  90.                     }
  91.                     if (row - 1 - matrixIndex < 0)
  92.                     {
  93.                         direction = 0;
  94.                         col++;
  95.                     }
  96.                     else
  97.                     {
  98.                         row--;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         Console.WriteLine();
  104.         Console.WriteLine();
  105.         Console.ReadLine();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement