Advertisement
Guest User

Matrix Shafling

a guest
May 22nd, 2019
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. class MatrixShuffling
  5. {
  6.     static void Main()
  7.     {
  8.         //Read the matrix dimentions from the console
  9.         var dim = Console.ReadLine()
  10.             .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  11.             .Select(long.Parse)
  12.             .ToArray();
  13.         var n = dim[0];
  14.         var m = dim[1];
  15.         var matrix = new string[n, m];
  16.  
  17.         //Fill the matrix
  18.         for (int i = 0; i < matrix.GetLength(0); i++)
  19.         {
  20.             var a = Console.ReadLine()
  21.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  22.                 .ToArray();
  23.  
  24.             for (int j = 0; j < matrix.GetLength(1); j++)
  25.             {
  26.                 matrix[i, j] = a[j];
  27.             }
  28.         }
  29.  
  30.         //var commands = new string[5];
  31.         while (true)
  32.         {
  33.             var commands = Console.ReadLine()
  34.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  35.                 .ToArray();
  36.             if (commands[0] == "swap")
  37.             {
  38.                 if (commands.Length == 5)
  39.                 {
  40.                     var y1 = long.Parse(commands[1]);
  41.                     var x1 = long.Parse(commands[2]);
  42.                     var y2 = long.Parse(commands[3]);
  43.                     var x2 = long.Parse(commands[4]);
  44.  
  45.                     if (!AreDimentionsValid(y1, x1, y2, x2, n, m))
  46.                     {
  47.                         Console.WriteLine("Invalid input!");
  48.                         continue;
  49.                     }
  50.  
  51.                     //Swapping algorithm
  52.                     string tempValue = matrix[y1, x1];
  53.                     matrix[y1, x1] = matrix[y2, x2];
  54.                     matrix[y2, x2] = tempValue;
  55.  
  56.                     //Call printing method
  57.                     PrintMatrix(matrix);
  58.                 }
  59.                 else
  60.                 {
  61.                     Console.WriteLine("Invalid input!");
  62.                     continue;
  63.                 }
  64.                
  65.             }
  66.             else if (commands[0] == "END")
  67.             {
  68.                 break;
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine("Invalid input!");
  73.                 continue;
  74.             }
  75.         }
  76.     }
  77.  
  78.     static bool AreDimentionsValid(long y1, long x1, long y2, long x2,
  79.         long rowLength, long colLength)
  80.     {
  81.         bool areValid = true;
  82.  
  83.         if (x1 < 0 || x1 >= colLength)
  84.         {
  85.             areValid = false;
  86.         }
  87.         else if (x2 < 0 || x2 >= colLength)
  88.         {
  89.             areValid = false;
  90.         }
  91.         else if (y1 < 0 || y1 >= rowLength)
  92.         {
  93.             areValid = false;
  94.         }
  95.         else if (y2 < 0 || y2 >= rowLength)
  96.         {
  97.             areValid = false;
  98.         }
  99.  
  100.         return areValid;
  101.     }
  102.  
  103.     static void PrintMatrix(string[,] matrix)
  104.     {
  105.         for (int i = 0; i < matrix.GetLength(0); i++)
  106.         {
  107.             for (int j = 0; j < matrix.GetLength(1); j++)
  108.             {
  109.                 Console.Write(matrix[i, j] + " ");
  110.             }
  111.             Console.WriteLine();
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement