Advertisement
stoianpp

Matrix1

May 11th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         int n = int.Parse(Console.ReadLine());
  7.         char type = char.Parse(Console.ReadLine());
  8.         var arr = new int[n, n];
  9.         switch (type)
  10.         {
  11.             case 'a': printA(arr); break;
  12.             case 'b': printB(arr); break;
  13.             case 'c': printC(arr); break;
  14.             case 'd': printD(arr); break;
  15.         }
  16.  
  17.         for (int i = 0; i < n; i++)
  18.         {
  19.             for (int j = 0; j < n; j++) Console.Write(arr[i, j]+(j+1<n?" ":""));
  20.             Console.WriteLine();
  21.         }
  22.     }
  23.  
  24.     static void printA(int[,] arr)
  25.     {
  26.         int counter = 1;
  27.         for (int i = 0; i < arr.GetLength(0); i++)
  28.         {
  29.             for (int j = 0; j < arr.GetLength(1); j++) arr[j, i] = counter++;
  30.         }
  31.     }
  32.  
  33.     static void printB(int[,] arr)
  34.     {
  35.         int counter = 1;
  36.         for (int i = 0; i < arr.GetLength(0); i++)
  37.         {
  38.             for (int j = 0; j < arr.GetLength(1); j++)
  39.             {
  40.                 if (i % 2 == 0) arr[j, i] = counter++;
  41.                 else { arr[arr.GetLength(1) -1 - j, i] = counter++; }
  42.             }
  43.         }
  44.     }
  45.  
  46.     static void printC(int[,] arr)
  47.     {
  48.         int number = arr.GetLength(0), counter = 1;
  49.         for (int i = number-1, x = 1; i >= 0; i--, x++)
  50.         {
  51.             for (int j = 0; j < x; j++)
  52.             {
  53.                 arr[i+j, j] = counter++;
  54.             }
  55.         }
  56.         for (int i = 1; i < number; i++)
  57.         {
  58.             for (int j = 0; j+i < number; j++)
  59.             {
  60.                 arr[j, i+j] = counter++;
  61.             }
  62.         }
  63.     }
  64.  
  65.     static void printD(int[,] matrix)
  66.     {
  67.         int x = 0, y = 0, direction = 2, counter = 1;
  68.         int number = matrix.GetLength(0);
  69.  
  70.         while (matrix[y, x] == 0)
  71.         {
  72.             matrix[y, x] = counter++;
  73.             switch (direction)
  74.             {
  75.                 case 2:
  76.                     if (y + 1 < number && matrix[y+1, x] == 0) y++;
  77.                     else { direction = 1; x++; };
  78.                     break;
  79.                 case 1:
  80.                     if (x + 1 < number && matrix[y, x+1] == 0) x++;
  81.                     else { direction = 3; y--; };
  82.                     break;
  83.                 case 4:
  84.                     if (x - 1 >= 0 && matrix[y, x - 1] == 0) x--;
  85.                     else { direction = 2; y++; };
  86.                     break;
  87.                 case 3:
  88.                     if (y - 1 >= 0 && matrix[y - 1, x] == 0) y--;
  89.                     else { direction = 4; x--; };
  90.                     break;
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement