Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class PrintMatrixD
- {
- static int n;
- static int counter = 0;
- static int[,] array;
- // Arranging the up side of the matrix
- static void UptPartSpiralMat(int rol, int col, int i,int[,] array)
- {
- if (array.Length == counter) return; // Stop cycle when counter is == array.length
- if (rol == i &&col == (i+1))
- {
- array[rol, col] = ++counter;
- return; // return to PrintSpiralMatrix
- }
- array[rol, col] = ++counter;// Input value
- UptPartSpiralMat(rol, col - 1, i, array); //Recursive evocation
- }
- // Arranging the right side of the matrix
- static void RightPartSpiralMat(int rol, int col, int i,int[,] array)
- {
- if (array.Length == counter) return; // Stop cycle if counter is == array.length
- if (rol == i) //Current max rol is 'i'
- {
- array[rol, col] = ++counter;
- UptPartSpiralMat(rol = i, col - 1, i, array);//Go to UptPartSpiralMat
- return;
- }
- array[rol, col] = ++counter;
- RightPartSpiralMat(rol - 1, col, i, array); //Recursive evocation RightPartSpiralMat
- }
- // Arranging the down side of the matrix
- static void DownPartSpiralMat(int rol, int col, int i,int[,] array)
- {
- if (array.Length == counter) return; // Stop cycle, after that print result
- if (col == n - i - 1) // Current max col
- {
- array[rol, col] = ++counter;
- RightPartSpiralMat(rol - 1, col, i, array); // Go to rihgt side
- return; // Stop
- }
- array[rol, col] = ++counter;
- DownPartSpiralMat(rol, col + 1, i, array); //Recursive evocation
- }
- // Arranging the left side of the matrix
- static void LeftPartSpiralMat(int rol, int col,int i, int[,]array )
- {
- if (array.Length == counter) return;
- if (rol == n - i - 1)
- {
- array[rol, col] = ++counter;
- DownPartSpiralMat(rol, col + 1, i,array);
- return;
- }
- array[rol, col] = ++counter;
- LeftPartSpiralMat(rol + 1, col, i,array);
- }
- static void PrintSpiralMatrix(int n)
- {
- int devider = n / 2;
- array = new int[n, n];
- //Solution
- for (int i = 0; i <= devider; i++)
- {
- LeftPartSpiralMat(i, i, i, array);
- }
- //Print array
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (array[i, j] > 9)
- {
- Console.Write(array[i, j] + " ");
- }
- else
- {
- Console.Write(array[i, j] + " ");
- }
- }
- Console.WriteLine();
- }
- }
- static void Main()
- {
- //Input
- n = int.Parse(Console.ReadLine());
- PrintSpiralMatrix(n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement