Advertisement
alidzhikov

MatrixShuffling

May 8th, 2015
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. class MatrixShuffling
  3. {
  4.     static void Main()
  5.     {
  6.         int rows = int.Parse(Console.ReadLine());
  7.         int cols = int.Parse(Console.ReadLine());
  8.  
  9.         string[,] matrix = new string[rows, cols];
  10.         for (int row = 0; row < rows; row++)
  11.         {
  12.             for (int col = 0; col < cols; col++)
  13.             {
  14.                 matrix[row, col] = Console.ReadLine();
  15.             }
  16.         }
  17.  
  18.         int x1 = 0;
  19.         int y1 = 0;
  20.         int x2 = 0;
  21.         int y2 = 0;
  22.         string temp = String.Empty;
  23.         string command = Console.ReadLine();
  24.         while (command != "END")
  25.         {
  26.             string[] commandTokens = command.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  27.             if (commandTokens.Length == 5 && commandTokens[0] == "swap")
  28.             {
  29.                 x1 = int.Parse(commandTokens[1]);
  30.                 y1 = int.Parse(commandTokens[2]);
  31.                 x2 = int.Parse(commandTokens[3]);
  32.                 y2 = int.Parse(commandTokens[4]);
  33.  
  34.                 if ((x1 >= 0 && x1 < rows) && (y1 >= 0 && y1 < cols)
  35.                     && (x2 >= 0 && x2 < rows) && (y2 >= 0 && y2 < cols))
  36.                 {
  37.                     temp = matrix[x1, y1];
  38.                     matrix[x1, y1] = matrix[x2, y2];
  39.                     matrix[x2, y2] = temp;
  40.  
  41.                     Console.WriteLine("(After swapping {0} and {1}): ", matrix[x2, y2], matrix[x1, y1]);
  42.                     for (int row = 0; row < rows; row++)
  43.                     {
  44.                         for (int col = 0; col < cols; col++)
  45.                         {
  46.                             Console.Write("{0,2} ", matrix[row, col]);
  47.                         }
  48.                         Console.WriteLine();
  49.                     }
  50.                 }
  51.                 else
  52.                 {
  53.                     Console.WriteLine("Invalid input!");
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 Console.WriteLine("Invalid input!");
  59.             }
  60.  
  61.             command = Console.ReadLine();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement