Advertisement
Guest User

Untitled

a guest
Sep 21st, 2015
140
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. 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.         int n = int.Parse(Console.ReadLine());
  10.         int m = int.Parse(Console.ReadLine());
  11.         string[,] matrix = new string[n, m];
  12.  
  13.         //Fill the matrix
  14.         for (int i = 0; i < n; i++)
  15.         {
  16.             for (int j = 0; j < m; j++)
  17.             {
  18.                 matrix[i, j] = Console.ReadLine();
  19.             }
  20.         }
  21.  
  22.         string[] commands = new string[5];
  23.         while (commands[0] != "END")
  24.         {
  25.             commands = Console.ReadLine().Split().ToArray();
  26.             if (commands[0] == "swap")
  27.             {
  28.                 int y1 = int.Parse(commands[1]);
  29.                 int x1 = int.Parse(commands[2]);
  30.                 int y2 = int.Parse(commands[3]);
  31.                 int x2 = int.Parse(commands[4]);
  32.  
  33.                 if(!AreDimentionsValid(y1, x1, y2, x2, n, m))
  34.                 {
  35.                     Console.WriteLine("Invalid input!");
  36.                     Console.WriteLine();
  37.                     continue;
  38.                 }
  39.                
  40.                 //Swapping algorithm
  41.                 string tempValue = matrix[y1, x1];
  42.                 matrix[y1, x1] = matrix[y2, x2];
  43.                 matrix[y2, x2] = tempValue;
  44.  
  45.                 //Call printing method
  46.                 PrintMatrix(matrix);
  47.             }
  48.             else if (commands[0] == "END")
  49.             {
  50.                 break;
  51.             }
  52.             else
  53.             {
  54.                 Console.WriteLine("Invalid Input!");
  55.                 Console.WriteLine();
  56.                 continue;
  57.             }
  58.         }
  59.     }
  60.  
  61.     static bool AreDimentionsValid(int y1, int x1, int y2, int x2,
  62.         int rowLength, int colLength)
  63.     {
  64.         bool areValid = true;
  65.  
  66.         if (x1 < 0 || x1 >= colLength)
  67.         {
  68.             areValid = false;
  69.         }
  70.         else if (x2 < 0 || x2 >= colLength)
  71.         {
  72.             areValid = false;
  73.         }
  74.         else if (y1 < 0 || y1 >= rowLength)
  75.         {
  76.             areValid = false;
  77.         }
  78.         else if (y2 < 0 || y2 >= rowLength)
  79.         {
  80.             areValid = false;
  81.         }
  82.  
  83.         return areValid;
  84.     }
  85.  
  86.     static void PrintMatrix(string[,] matrix)
  87.     {
  88.         for (int i = 0; i < matrix.GetLength(0); i++)
  89.         {
  90.             for (int j = 0; j < matrix.GetLength(1); j++)
  91.             {
  92.                 Console.Write(" " + matrix[i, j] + "\t");
  93.             }
  94.             Console.WriteLine();
  95.         }
  96.         Console.WriteLine();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement