Advertisement
dimipan80

6.13Loops_SpiralMatrix_Reverse

Mar 25th, 2014
113
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.  
  4. class ReverseSpiralMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("Enter a positive whole number in the range [1 ... 31], for N = ");
  9.         string numberStr = Console.ReadLine();
  10.         int numN = int.Parse(numberStr);
  11.        
  12.         if (numN < 1 || numN > 31)
  13.         {
  14.             Console.WriteLine("Error - The number is Out of Range !!!");
  15.         }
  16.         else
  17.         {
  18.             int[,] matrix = new int[numN, numN];
  19.             int row = 0;
  20.             int col = 0;
  21.             string direction = "right";
  22.             int maxValue = numN * numN;
  23.  
  24.             for (int i = maxValue; i >= 1; i--)
  25.             {
  26.                 if (direction == "right" && (col > (numN - 1) || matrix [row, col] != 0))
  27.                 {
  28.                     direction = "down";
  29.                     row++;
  30.                     col--;
  31.                 }
  32.                 if (direction == "down" && (row > (numN - 1) || matrix [row, col] != 0))
  33.                 {
  34.                     direction = "left";
  35.                     row--;
  36.                     col--;
  37.                 }
  38.                 if (direction == "left" && (col < 0 || matrix [row, col] != 0))
  39.                 {
  40.                     direction = "up";
  41.                     row--;
  42.                     col++;
  43.                 }
  44.                 if (direction == "up" && (row < 0 || matrix[row, col] != 0))
  45.                 {
  46.                     direction = "right";
  47.                     row++;
  48.                     col++;
  49.                 }
  50.  
  51.                 matrix [row, col] = i;
  52.                 if (direction == "right")
  53.                 {
  54.                     col++;
  55.                 }
  56.                 if (direction == "down")
  57.                 {
  58.                     row++;
  59.                 }
  60.                 if (direction == "left")
  61.                 {
  62.                     col--;
  63.                 }
  64.                 if (direction == "up")
  65.                 {
  66.                     row--;
  67.                 }
  68.             }
  69.  
  70.             // Print the numbers in reverse order to spiral matrix:
  71.             Console.WriteLine("The numbers in Reverse Spiral Matrix are:");
  72.             for (int r = 0; r < numN; r++)
  73.             {
  74.                 for (int c = 0; c < numN; c++)
  75.                 {
  76.                     Console.Write("{0,4} ", matrix [r, c]);
  77.                 }
  78.                 Console.WriteLine();
  79.             }
  80.         }
  81.         Console.ReadLine();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement