Advertisement
soxa

MatrixD

Dec 24th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2.  
  3. class PrintMatrixD
  4. {
  5.     static int n;
  6.     static int counter = 0;
  7.     static int[,] array;
  8.     // Arranging the up side of the matrix
  9.     static void UptPartSpiralMat(int rol, int col, int i,int[,] array)
  10.     {
  11.         if (array.Length == counter) return; // Stop cycle when counter is == array.length
  12.         if (rol == i &&col == (i+1))
  13.         {
  14.             array[rol, col] = ++counter;
  15.             return; // return to PrintSpiralMatrix
  16.         }
  17.         array[rol, col] = ++counter;// Input value
  18.         UptPartSpiralMat(rol, col - 1, i, array); //Recursive evocation
  19.     }
  20.     // Arranging the right side of the matrix
  21.     static void RightPartSpiralMat(int rol, int col, int i,int[,] array)
  22.     {
  23.         if (array.Length == counter) return; // Stop cycle if counter is == array.length
  24.         if (rol == i) //Current max rol is 'i'
  25.         {
  26.             array[rol, col] = ++counter;
  27.             UptPartSpiralMat(rol = i, col - 1, i, array);//Go to UptPartSpiralMat
  28.             return;
  29.         }
  30.         array[rol, col] = ++counter;
  31.         RightPartSpiralMat(rol - 1, col, i, array); //Recursive evocation RightPartSpiralMat
  32.     }
  33.     // Arranging the down side of the matrix
  34.     static void DownPartSpiralMat(int rol, int col, int i,int[,] array)
  35.     {
  36.         if (array.Length == counter) return; // Stop cycle, after that print result
  37.         if (col == n - i - 1) // Current max col
  38.         {
  39.             array[rol, col] = ++counter;
  40.             RightPartSpiralMat(rol - 1, col, i, array); // Go to rihgt side
  41.             return; // Stop
  42.         }
  43.         array[rol, col] = ++counter;
  44.         DownPartSpiralMat(rol, col + 1, i, array); //Recursive evocation
  45.     }
  46.     // Arranging the left side of the matrix
  47.     static void LeftPartSpiralMat(int rol, int col,int i, int[,]array )
  48.     {
  49.         if (array.Length == counter) return;
  50.         if (rol == n - i - 1)
  51.         {
  52.             array[rol, col] = ++counter;
  53.             DownPartSpiralMat(rol, col + 1, i,array);
  54.             return;
  55.         }
  56.         array[rol, col] = ++counter;
  57.         LeftPartSpiralMat(rol + 1, col, i,array);
  58.     }
  59.    
  60.     static void PrintSpiralMatrix(int n)
  61.     {
  62.         int devider = n / 2;
  63.         array = new int[n, n];
  64.         //Solution
  65.         for (int i = 0; i <= devider; i++)
  66.         {
  67.             LeftPartSpiralMat(i, i, i, array);
  68.         }
  69.         //Print array
  70.         for (int i = 0; i < n; i++)
  71.         {
  72.             for (int j = 0; j < n; j++)
  73.             {
  74.                 if (array[i, j] > 9)
  75.                 {
  76.                     Console.Write(array[i, j] + " ");
  77.                 }
  78.                 else
  79.                 {
  80.                     Console.Write(array[i, j] + "  ");
  81.                 }
  82.             }
  83.             Console.WriteLine();
  84.         }
  85.     }
  86.     static void Main()
  87.     {
  88.         //Input
  89.         n = int.Parse(Console.ReadLine());
  90.         PrintSpiralMatrix(n);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement