Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- class MatrixShuffling
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int m = int.Parse(Console.ReadLine());
- string[,] matrix = new string[n, m];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- Console.WriteLine("Please enter element [{0},{1}]", i, j);
- matrix[i, j] = Console.ReadLine();
- }
- }
- string command = Console.ReadLine();
- while (command != "END")
- {
- string[] commandsSplit = command.Split();
- int x1, x2, y1, y2;
- try
- {
- x1 = int.Parse(commandsSplit[1]);
- y1 = int.Parse(commandsSplit[2]);
- x2 = int.Parse(commandsSplit[3]);
- y2 = int.Parse(commandsSplit[4]);
- if (commandsSplit[0] != "swap" || !Enumerable.Range(0,n).Contains(x1) || !Enumerable.Range(0,n).Contains(x2)
- || !Enumerable.Range(0,m).Contains(y1) || !Enumerable.Range(0,m).Contains(y2))
- {
- throw new ArgumentOutOfRangeException();
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Invalid input!");
- command = Console.ReadLine();
- continue;
- }
- string temp = matrix[x1, y1];
- matrix[x1, y1] = matrix[x2, y2];
- matrix[x2, y2] = temp;
- PrintMatrix(matrix, n, m);
- command = Console.ReadLine();
- }
- }
- private static void PrintMatrix(string[,] matrix, int n, int m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- Console.Write("{0} ", matrix[i, j]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement