marekov

SpiralMatrix

Jun 30th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SpiralMatrix
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int size = int.Parse(Console.ReadLine());
  12.             int[,] matrix = new int[size, size];
  13.             int index = 0;
  14.             int x = 0;
  15.             int square = (size * size);
  16.             bool move = true;
  17.  
  18.             while (move)
  19.             {
  20.                 ToRight(size, matrix, index, ref x, square, ref move);
  21.                 ToBottom(size, matrix, index, ref x, square, ref move);
  22.                 ToLeft(size, matrix, index, ref x, square, ref move);
  23.                 ToUp(size, matrix, index, ref x, square, ref move);
  24.                 index++;
  25.             }
  26.  
  27.             Print(size, matrix);
  28.         }
  29.  
  30.         private static void ToUp(int size, int[,] matrix, int idx, ref int x, int sq, ref bool move)
  31.         {
  32.             for (int col = idx; col < idx + 1; col++)
  33.             {
  34.                 for (int row = size - 2 - idx; row > idx; row--)
  35.                 {
  36.                     x++;
  37.                     matrix[row, col] = x;
  38.                     if (x == sq)
  39.                     {
  40.                         move = false;
  41.                         break;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.         private static void ToLeft(int size, int[,] matrix, int idx, ref int x, int sq, ref bool move)
  48.         {
  49.             for (int row = size - 1 - idx; row > size - 2 - idx; row--)
  50.             {
  51.                 for (int col = size - 2 - idx; col >= 0 + idx; col--)
  52.                 {
  53.                     x++;
  54.                     matrix[row, col] = x;
  55.                     if (x == sq)
  56.                     {
  57.                         move = false;
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private static void ToBottom(int size, int[,] matrix, int idx, ref int x, int sq, ref bool move)
  65.         {
  66.             for (int col = size - 1 - idx; col > size - 2 - idx; col--)
  67.             {
  68.                 for (int row = 1 + idx; row < size - idx; row++)
  69.                 {
  70.                     x++;
  71.                     matrix[row, col] = x;
  72.                     if (x == sq)
  73.                     {
  74.                         move = false;
  75.                         break;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.  
  81.         private static void ToRight(int size, int[,] matrix, int idx, ref int x, int sq, ref bool move)
  82.         {
  83.             for (int row = idx; row < idx + 1; row++)
  84.             {
  85.                 for (int col = idx; col < size - idx; col++)
  86.                 {
  87.                     x++;
  88.                     matrix[row, col] = x;
  89.                     if (x == sq)
  90.                     {
  91.                         move = false;
  92.                         break;
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private static void Print(int size, int[,] matrix)
  99.         {
  100.             for (int row = 0; row < size; row++)
  101.             {
  102.                 for (int col = 0; col < size; col++)
  103.                 {
  104.                     Console.Write(matrix[row, col] + " ");
  105.                 }
  106.                 Console.WriteLine();
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment